file bug???

Andrew Bennetts andrew-pythonlist at puzzling.org
Fri Nov 21 05:18:31 CET 2003


On Thu, Nov 20, 2003 at 10:52:40PM -0500, hokiegal99 wrote:
> When I use 'file' instead of 'open' on the 4th line of this script (the 
> line that begins with "outputFile") I get this error: 
> "UnboundLocalError: local variable 'file' referenced before assignment" 
> 'open' works w/o problem and 'file' works in some other scripts that are 
> almost identical to this one... any ideas? I can post a script where 
> 'file' works if anyone is interested.
> 
> import os, string
> setpath = raw_input("Enter the path to the Mac files and folders: ")
> def clean_spaces(setpath):
>    outputFile = open('fix-spaces.txt', 'w')
[...]
>        for file in files:
             ^^^^
             This is your problem.

You're assigning to file here, so file is a local variable in this function,
not the builtin you're expecting.  This is why your variable names shouldn't
"shadow" the builtins -- you get confusing errors like this one.

-Andrew.






More information about the Python-list mailing list