Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > file bug???

Reply
Thread Tools

file bug???

 
 
Andrew Bennetts
Guest
Posts: n/a
 
      11-21-2003
(please don't top-post)

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


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


 
Reply With Quote
 
 
 
 
hokieghal99
Guest
Posts: n/a
 
      11-21-2003
I see. I understand now. Thank you for the advice.

Bengt Richter wrote:
> On Thu, 20 Nov 2003 22:52:40 -0500, hokiegal99 <(E-Mail Removed)> 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.

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


 
Reply With Quote
 
 
 
 
Anand Pillai
Guest
Posts: n/a
 
      11-24-2003
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

Andrew Bennetts <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> (please don't top-post)
>
> > > 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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting JPG file ppt file [Powerpoint] file zxcvar Digital Photography 7 06-22-2009 07:54 PM
Reading of file by next of map file and by next of file descriptor. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 1 07-10-2007 02:46 AM
How to create MDF file (.mdf) file from XML file. Dave ASP .Net 1 06-07-2007 11:32 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM



Advertisments