views:

71

answers:

2

Im using the plone cms and am having trouble with a python script. I get a name error "the global name 'open' is not defined". When i put the code in a seperate python script it works fine and the information is being passed to the python script becuase i can print the query. Code is below:

#Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE = request.RESPONSE



# Insert data that was passed from the form
query=request.query
#print query


f = open("blast_query.txt","w")
for i in query:
    f.write(i)

return printed

I also have a second question, can i tell python to open a file in in a certain directory for example, If the script is in a certain loaction i.e. home folder, but i want the script to open a file at home/some_directory/some_directory can it be done?

A: 

For the first part: Check to make sure open() isn't being shadowed somehow by a variable that was declared with the global keyword but isn't actually defined in the global scope.

For the second (assuming you actually have access to the file system and methods/modules that can manipulate it): If the script is in home, Just do f = open('some_directory/some_directory/file_you_want_to_open.txt', 'w'). If the directories don't exist, however, you can just catch the error that open() will throw, use os.makedirs() to create the subdirectories, and then use open() again. Example:

import os
import os.path

try:
    path = 'some_directory/some_directory'
    fname = 'file_you_want_to_open.txt'
    f = open(os.path.join(path, fname), 'w')
except IOError:
    os.makedirs(path)
    f = open(os.path.join(path, fname), 'w')

And that will create the leaf directory and the necessary intermediate directories if those directories are not already present, and then open the wanted file (or create a new one if the file doesn't already exist).

...Though there's a slightly simpler way to do it, as I just discovered.

import os
import os.path

path = 'some_directory/some_directory'
fname = 'file_you_want_to_open.txt'

if not os.access(path, os.F_OK):
    os.makedirs(path)

f = open(os.path.join(path, fname), 'w')

http://docs.python.org/library/os.html#os.access

(Note that while I used '/' in the string literals for the path name, you should probably use the stuff in os and os.path to handle it more flexibly.)

JAB
Python Scripts in Plone run a restricted set of python and thus the open call isn't available.
Martijn Pieters
Ah, I see. So much for that, then.
JAB
+3  A: 

Python Scripts in Plone are restricted and have no access to the filesystem. The open call is thus not available. You'll have to use an External Method or full python module to have full access to the filesystem.

Martijn Pieters
Exactly. You can check this link from Plone for more information: http://plone.org/documentation/faq/restricted-python-scripts
Somebody still uses you MS-DOS
Just read up on the external methods, could you elaborate of the full python module?
Grenko