file bug???

Discussion in 'Python' started by hokiegal99, Nov 21, 2003.

  1. hokiegal99

    hokiegal99 Guest

    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)
     
    hokiegal99, Nov 21, 2003
    #1
    1. Advertising

  2. hokiegal99

    Ben Finney Guest

    On Thu, 20 Nov 2003 22:52:40 -0500, hokiegal99 wrote:
    > 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.

    --
    \ "Quote me as saying I was mis-quoted." -- Groucho Marx |
    `\ |
    _o__) |
    Ben Finney <http://bignose.squidly.org/>
     
    Ben Finney, Nov 21, 2003
    #2
    1. Advertising

  3. On Thu, 20 Nov 2003 22:52:40 -0500, hokiegal99 <> 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
     
    Bengt Richter, Nov 21, 2003
    #3
  4. On Thu, Nov 20, 2003 at 10:52:40PM -0500, hokiegal99 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.
    >
    > 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.
     
    Andrew Bennetts, Nov 21, 2003
    #4
  5. hokiegal99

    Anand Pillai Guest

    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 <> wrote in message news:<>...
    > 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)
     
    Anand Pillai, Nov 21, 2003
    #5
  6. 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.

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Bestiaria Home Page: http://www.beastie.dm.net/ <
    > Home Page: http://www.dm.net/~wulfraed/ <
     
    Dennis Lee Bieber, Nov 21, 2003
    #6
  7. hokiegal99

    Peter Otten Guest

    Anand Pillai wrote:

    > 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.
    >>> import os
    >>> os.walk

    <function walk at 0x40279a3c>
    >>>


    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
     
    Peter Otten, Nov 21, 2003
    #7
  8. hokiegal99

    Matt Goodall Guest

    Anand Pillai wrote:

    >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

    --
    Matt Goodall, Pollenation Internet Ltd
    w: http://www.pollenation.net
    e:
     
    Matt Goodall, Nov 21, 2003
    #8
  9. (Anand Pillai) wrote in message news:<>...

    > 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.
     
    David M. Wilson, Nov 21, 2003
    #9
  10. hokiegal99

    Anand Pillai Guest

    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


    On Fri, Nov 21, 2003 at 12:00:21AM -0800, Anand Pillai wrote:
    >
    > 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.
     
    Anand Pillai, Nov 21, 2003
    #10
  11. (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.
     
    Andrew Bennetts, Nov 21, 2003
    #11
  12. hokiegal99

    hokieghal99 Guest

    I see. I understand now. Thank you for the advice.

    Bengt Richter wrote:
    > On Thu, 20 Nov 2003 22:52:40 -0500, hokiegal99 <> 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
     
    hokieghal99, Nov 21, 2003
    #12
  13. hokiegal99

    Anand Pillai Guest

    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 <> wrote in message news:<>...
    > (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.
     
    Anand Pillai, Nov 24, 2003
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Re: BUG? OR NOT A BUG?

    John, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    587
  2. RedEye
    Replies:
    2
    Views:
    615
    Jason Kester
    Dec 13, 2005
  3. Michel Joly de Lotbiniere

    Bug Parade Bug 4953793

    Michel Joly de Lotbiniere, Nov 30, 2003, in forum: Java
    Replies:
    4
    Views:
    670
    Michel
    Dec 2, 2003
  4. DarkSpy
    Replies:
    4
    Views:
    915
    tom_usenet
    Jun 27, 2003
  5. Alan Davies
    Replies:
    7
    Views:
    237
    Alan Davies
    Nov 27, 2003
Loading...

Share This Page