file bug???

H

hokiegal99

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)
 
B

Ben Finney

When I use 'file' instead of 'open' on the 4th line of this script
(the line that begins with "outputFile")

Please post a minimal script that shows the problem. This one has loads
of irrelevant guff.
I get this error: "UnboundLocalError: local variable 'file' referenced
before assignment"

If you're using the function 'file', it seems best not to have a
variable by that name also.

It will be easier to see what's going on if you show a smaller script
that does nothing unrelated to the problem behaviour. More than likely,
you'll see the cause of the problem yourself that way.
 
B

Bengt Richter

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.

No, UIAM this is a good lesson on why it's not a good idea to use names of builtins as
variable names.
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')
^^^^-- file would be fine here if you didn't shadow the name below
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:
^^^^ this is effectively an assignment in the local scope, so the compiler
thinks 'file' is a local variable (which it actually becomes because of this).
That means that 'file' in the place of 'open' above can't be referring
to the intended builtin, and so it looks like it's referring to the 'file'
variable you assign here, but before it's assigned.
Try using a different name, e.g.,
for a_file in files:
old_fname = file
old_fname = a_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)

Regards,
Bengt Richter
 
A

Andrew Bennetts

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.
 
A

Anand Pillai

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
 
D

Dennis Lee Bieber

hokiegal99 fed this fish to the penguins on Thursday 20 November 2003
19:52 pm:

"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.

for file in files:

You just created a local 'variable' named file, this local is local to
the entire def, and will mask out the file() system call.

--
 
P

Peter Otten

Anand said:
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.

Python 2.3.2 (#1, Oct 21 2003, 10:03:19)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Welcome to Python 2.3 :)

IMHO the new os.walk() generator is *much* better than os.path.walk() and I
expect that os.path.walk() will no longer occur in new code.
Most common usage pattern:

for dirpath, dirnames, filenames in os.walk(rootdir):
# proces files/subdirectories here

The gain in explicitness is comparable to list comprehension vs map().

Peter
 
M

Matt Goodall

Anand said:
There are many problems with your code.


[snip]

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.
os.walk is fine, it was added in 2.3.

Cheers, Matt
 
D

David M. Wilson

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 by 'keyword' you mean 'language keyword', then you are wrong.
Assignments to keywords obviously do not work. Assignments to names
from __builtins__ does.

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.

It does now. Read the current Python documentation.


David.
 
A

Anand Pillai

It does not exist in Python 2.2, AFAIK.
It is a good idea to write code that at least works for
one previous Python version, hence the comment.

-Anand


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.

It does exist in Python 2.3, and it does take one argument (and optionally
others):

http://python.org/doc/lib/os-file-dir.html#l2h-1473

-Andrew.
 
A

Andrew Bennetts

(please don't top-post)
It does not exist in Python 2.2, AFAIK.
It is a good idea to write code that at least works for
one previous Python version, hence the comment.

You should have said so in the first place -- explicit is better than
implicit :)

-Andrew.
 
A

Anand Pillai

I tend to be brief while posting to newsgroups.
Most of the time, you get lambasted for verbosity here.
Hence I tend to stick to stuff and trust pythonistas
for their knowledge and intuition ;-)

-Anand
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top