# is file access was forbidden in python plugins?

**URL:** <https://forums.plex.tv/t/is-file-access-was-forbidden-in-python-plugins/90144>\
**Category:** Dev/API Corner\
**Tags:** scanner-agent-dev\
**Created:** [January 15, 2015, 7:31am UTC](https://forums.plex.tv/t/is-file-access-was-forbidden-in-python-plugins/90144 "2015-01-15T07:31:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![w1n2kp](https://avatars.discourse-cdn.com/v4/letter/w/c57346/32.png) [@w1n2kp](https://forums.plex.tv/u/w1n2kp)\
**Post date:** [January 15, 2015, 7:31am UTC](https://forums.plex.tv/t/is-file-access-was-forbidden-in-python-plugins/90144/1 "2015-01-15T07:31:22Z")

</div>

Hi,

I was trying to write a plugin which grab movie information from Internet and save them into a customized XML file with the movie file together for future reading.

&nbsp;

but seems I can't use either "open()" and "file()" function in the scripts. please check the log below

&nbsp;

file1 = file(path, "w")

NameError: global name "file" is not defined

&nbsp;

and when I change the calling into "open()" I got the same error message

&nbsp;

Any one have idea with it?

&nbsp;

Thanks a lot

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![BrigoNortensson](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/brigonortensson/32/93940_2.png) [@BrigoNortensson](https://forums.plex.tv/u/BrigoNortensson)\
**Post date:** [January 16, 2015, 6:56pm UTC](https://forums.plex.tv/t/is-file-access-was-forbidden-in-python-plugins/90144/2 "2015-01-16T18:56:27Z")

</div>

I'm only now just starting to learn Python... but don't you have to import some module for that?&nbsp;

---

<div class="post-metadata">

**Author:** ![ZeroQI](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/zeroqi/32/6522_2.png) [@ZeroQI](https://forums.plex.tv/u/ZeroQI)\
**Post date:** [April 6, 2015, 11:37am UTC](https://forums.plex.tv/t/is-file-access-was-forbidden-in-python-plugins/90144/3 "2015-04-06T11:37:37Z")

</div>

"Plugin" is a bit evasive. it's an agent which follows a specifically defined framework [dev.plexapp.com/docs/api/datakit.html]&nbsp;or a scanner which can't use theses functions. &nbsp;If you manage a metadata agent, it's in the wrong category (scanner)

Agents can't write to the video files folder but can read it. Agents can however write to the agent data folder but not create new folders, need to be done manually. Hama agent write to the agent data folder for xml and picture cache. Check Hama source code for Data.load like:

```

```

error\_log['log\_file']=["log1", "log 2", log 3]

&nbsp;

&nbsp;### HAMA - Load logs, add non-present entried then Write log files to Plug-in /Support/Data/com.plexapp.agents.hama/DataItems ###

&nbsp; &nbsp; for log in error\_log:

&nbsp; &nbsp; &nbsp; if error\_log[log] != []:

&nbsp; &nbsp; &nbsp; &nbsp; if not Data.Exists(log+".htm"):

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string=""

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if log == 'TVDB posters missing': string = WEB\_LINK % ("[http://thetvdb.com/wiki/index.php/Posters",](http://thetvdb.com/wiki/index.php/Posters)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Restrictions") + "  
" 

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if log == 'Plex themes missing': &nbsp;string = WEB\_LINK % ("[https://plexapp.zendesk.com/hc/en-us/articles/201572843","Restrictions")](https://plexapp.zendesk.com/hc/en-us/articles/201572843)+ "  
" 

&nbsp; &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string = Data.Load(log+".htm")

&nbsp; &nbsp; for entry in error\_log[log]:

&nbsp; &nbsp; if entry not in string: &nbsp;Data.Save(log+".htm", string + entry + "  
")

Scanners generally don't write files. My "Absolute series scanner" is the only one i saw to do such thing. It can write to the hdd using:

```
### Log function ########################################################################################
def Log(entry, filename='Plex Media Scanner Custom.log'): #need relative path
  #Logging = [['Synology',          "/volume1/Plex/Library/Application Support/Plex Media Server/Logs/"],
  #            ['Synology2',         "../../Plex/Library/Application Support/Plex Media Server/Logs/"],
  #            ['Ubuntu-10.04',      "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Logs/"]
  #            ['windows Vista/7/8', "C:\\Users\\Default\\AppData\\Local\\Plex Media Server\\Logs\\"],
  #            ['Qnap',              ""]
  #          ]
  try: 
    with open(filename, 'a') as file: #with open("/volume1/Plex/Library/Application Support/Plex Media Server/Logs/" + filename, 'a') as file:
      file.write( entry + "
" ) #
 make it readable in notepad under windows directly
      print line + "
"
  except:  pass
```

You call it like:&nbsp;Log("Scan: (root: '%s', path='%s', subdirs: %s, language: '%s')" % (root, path, str(subdirs), language)) from scanner side

---

<div class="post-metadata">

**Author:** ![agentplex007](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@agentplex007](https://forums.plex.tv/u/agentplex007)\
**Post date:** [April 9, 2015, 5:32pm UTC](https://forums.plex.tv/t/is-file-access-was-forbidden-in-python-plugins/90144/4 "2015-04-09T17:32:01Z")

</div>

open() is not available when running inside the Plugin sandbox environment. You can use os.open() instead

Here's the code to read a file and return the content as a string

```
def readFile(filename):
    '''Read a file and return its content
    This is used when running under Agent as open() is not
    available for use.
    @param filename  file to read
    @return          file content or None
    '''
    size = os.path.getsize(filename)
    fd = os.open(filename, os.O_RDONLY)
    data = os.read(fd, size)
    return data
```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 21, 2019, 4:12am UTC](https://forums.plex.tv/t/is-file-access-was-forbidden-in-python-plugins/90144/5 "2019-12-21T04:12:33Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
