file bug???
Anand Pillai
pythonguy at Hotpop.com
Fri Nov 21 03:00:21 EST 2003
There are many problems with your code.
First of all answer to your main question.
In Line 15, you are using a local variable named 'file'
which is being used to walk a list of files. When you
are using a keyword as a variable, python gets confused
and thinks that you are trying to use a variable before it
is being defined.
If you replace the 'file' in L15 with say 'f', you can use
'file' instead of 'open' in L4.
Some other things.
o 'os' module does not have a function 'walk'. Change it to os.path.walk .
o os.path.walk takes 3 arguments, not one. You need to correct this.
-Anand
hokiegal99 <hokiegal99 at hotmail.com> wrote in message news:<3FBD8C08.9010002 at hotmail.com>...
> 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 root, dirs, files in os.walk(setpath):
> for dir in dirs:
> old_dname = dir
> new_dname = old_dname.strip()
> if new_dname != old_dname:
> newpath = os.path.join(root,new_dname)
> oldpath = os.path.join(root,old_dname)
> print >> outputFile, "Replaced ", old_dname, "\nWith
> ", new_dname
> os.rename(oldpath,newpath)
> for file in files:
> old_fname = file
> new_fname = old_fname.strip()
> if new_fname != old_fname:
> newpath = os.path.join(root,new_fname)
> oldpath = os.path.join(root,old_fname)
> print >> outputFile, "Replaced ", old_fname, "\nWith
> ", new_fname
> os.rename(oldpath,newpath)
> outputFile.close()
> clean_spaces(setpath)
More information about the Python-list
mailing list