file bug???

hokiegal99 hokiegal99 at hotmail.com
Fri Nov 21 04:52:40 CET 2003


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