Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Opening File

Status
Not open for further replies.

Lozbinator

Programmer
Jan 13, 2003
50
AU
Hi All,

Can anybody suggest why the following line of code:

myfile = open('folders2.txt', 'r')

might create this error:

Name Error: global name 'open' is not defined

Thanks!
 
Looks like your Python installation may have a problem.

"open" is part of the "__built-ins__" module that is automatically loaded when python starts unless your installation of python has a glitch.

You can verify this by using the "dir" built-in command to look for the "__built-ins__" module after starting python. For example,

$ python
Python 2.2.1 (#1, Jun 25 2002, 10:55:46)
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> dir ()
['__builtins__', '__doc__', '__name__']


The above shows that "__builtins__" is available. Running the "dir" command on "__built-ins__" will show all of the attributes, functions, etc. that are associated with that module:

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'IOError', 'ImportError', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'OverflowWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeError', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', 'abs', 'apply', 'bool', 'buffer', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'round', 'setattr', 'slice', 'staticmethod', 'str', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
>>>

In the alphabetized list you can see that "open" is loaded and ready for action.

If you don't find "__built-ins__", then something is wrong with your Python installation.

Hope that helps.

ZaSter

 
Thank you for your reply.

I am trying to use this script in Plone, so I will check on the Python modules installed and available to it.

Unless anybody has any ideas why Plone would dislike me using 'open'??
 
Did not know about Plone until you mentioned it, so there could be something in the Plone documentation that you may have missed.

In the meantime, why not give the following a try. Add the following statement to the top of your script:

from __built-ins__ import *

Does that help?

 
If you can create and post a "test case" - the simplest script you can get which still demonstrates the problem, then it'll be far easier to identify and fix the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top