variable assignment within a loop
Michael Surette
msurette at myrealbox.com
Mon Nov 3 16:39:55 EST 2003
On Mon, 03 Nov 2003 15:37:28 -0500, hokieghal99 wrote:
> I have this for loop in a program that I'm writing:
>
> for bad_dir_char in bad_dir_chars:
> newdir = dir.replace(bad_dir_char,'-')
>
> Now, when I use this if statement:
>
> if newdir != dir:
> old_dir_path = os.path.join(root,dir)
> new_dir_path = os.path.join(root,newdir)
> os.rename(old_dir_path,new_dir_path)
> print "replaced: ",bad_dir_char,"
>
> I get a "local variable 'newdir' referenced before assignment" error.
This has nothing to do with where variables live. What is happening is
when bad_dir_chars is empty, newdir doesn't get assigned in your for loop.
More information about the Python-list
mailing list