Beginner question: use function to read text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luke

    #1

    Beginner question: use function to read text file

    I'm pretty stuck at the moment and wondering if anyone can spot the problem.
    Trying to create a function that will read a text file into a list and
    return that list.

    I wrote the following function and saved it as 'fileloader.py'

    def fileload(fname) :
    infile=open(fna me)
    dates =[]
    times=[]
    open=[]
    high=[]
    low=[]
    close=[]
    vol=[]
    count=0
    for line in infile:
    item=line.split ()
    dates.append(it em[0])
    times.append(it em[1])
    open.append(ite m[2])
    high.append(ite m[3])
    low.append(item[4])
    close.append(it em[5])
    vol.append(item[6])
    #print
    dates[count],times[count],open[count],high[count],low[count],vol[count]
    count=count+1


    return dates,times,ope n,high,low,clos e


    Then I executed the following script (merge contract v1.py):

    import fileloader
    filename='c:/Python24/test/testdata2.txt'
    fileloader.file load(filename)


    I then get the following error messages:

    Traceback (most recent call last)
    File "C:\Python24\te st\merge contract v1.py", in line3, in?
    fileloader.file load(filename)
    File ("C:\Python24\t ext\fileloader. py", in line2, in fileload
    infile=open(fna me)
    UnboundLocalErr or: local variable 'open' referenced before assignment
    Script terminated

    Thanks for any help,
    Luke


  • James Stroud

    #2
    Re: Beginner question: use function to read text file

    Luke wrote:[color=blue]
    > I'm pretty stuck at the moment and wondering if anyone can spot the problem.
    > Trying to create a function that will read a text file into a list and
    > return that list.
    >
    > I wrote the following function and saved it as 'fileloader.py'
    >
    > def fileload(fname) :
    > infile=open(fna me)
    > dates =[]
    > times=[]
    > open=[]
    > high=[]
    > low=[]
    > close=[]
    > vol=[]
    > count=0
    > for line in infile:
    > item=line.split ()
    > dates.append(it em[0])
    > times.append(it em[1])
    > open.append(ite m[2])
    > high.append(ite m[3])
    > low.append(item[4])
    > close.append(it em[5])
    > vol.append(item[6])
    > #print
    > dates[count],times[count],open[count],high[count],low[count],vol[count]
    > count=count+1
    >
    >
    > return dates,times,ope n,high,low,clos e
    >
    >
    > Then I executed the following script (merge contract v1.py):
    >
    > import fileloader
    > filename='c:/Python24/test/testdata2.txt'
    > fileloader.file load(filename)
    >
    >
    > I then get the following error messages:
    >
    > Traceback (most recent call last)
    > File "C:\Python24\te st\merge contract v1.py", in line3, in?
    > fileloader.file load(filename)
    > File ("C:\Python24\t ext\fileloader. py", in line2, in fileload
    > infile=open(fna me)
    > UnboundLocalErr or: local variable 'open' referenced before assignment
    > Script terminated
    >
    > Thanks for any help,
    > Luke
    >
    >[/color]

    def fileload(fname) :
    infile=open(fna me) # <===
    dates =[]
    times=[]
    open=[] # <===

    You have assigned open (which, by the way, is a builtin!) in a function
    *after* you have referenced it. You have over-ridden the open name with
    the assignment, but you have referenced it 'before assignment', as your
    error mesage says. This is a favorite trip-up of newer pythong
    programmers. Perhaps replace

    open=[]

    with something like

    open_=[]

    etc.

    James



    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095


    Comment

    • Gary Herron

      #3
      Re: Beginner question: use function to read text file

      Luke wrote:[color=blue]
      > I'm pretty stuck at the moment and wondering if anyone can spot the problem.
      > Trying to create a function that will read a text file into a list and
      > return that list.
      >
      > I wrote the following function and saved it as 'fileloader.py'
      >
      > def fileload(fname) :
      > infile=open(fna me)
      > dates =[]
      > times=[]
      > open=[]
      >[/color]
      Here's the problem. You are using the name "open" it two contexts. The
      variable named "open" is a local variable, and as such it hides the
      builtin function of the same name used to open files. (It does not
      matter that the use of "open" as a function precedes the use of "open"
      as a variable name. Python is rather strict about this. If you use a
      variable in a def, then ANYWHERE in that def, that name refers to the
      local variable and hides any uses of the name from enclosing scopes.)
      If that last makes sense, good. If not, then just follow this rule:
      Choose your variable names to be different than any builtin names -- or
      at least different than any of the builtin names you intend to use.

      Gary Herron
      [color=blue]
      > high=[]
      > low=[]
      > close=[]
      > vol=[]
      > count=0
      > for line in infile:
      > item=line.split ()
      > dates.append(it em[0])
      > times.append(it em[1])
      > open.append(ite m[2])
      > high.append(ite m[3])
      > low.append(item[4])
      > close.append(it em[5])
      > vol.append(item[6])
      > #print
      > dates[count],times[count],open[count],high[count],low[count],vol[count]
      > count=count+1
      >
      >
      > return dates,times,ope n,high,low,clos e
      >
      >
      > Then I executed the following script (merge contract v1.py):
      >
      > import fileloader
      > filename='c:/Python24/test/testdata2.txt'
      > fileloader.file load(filename)
      >
      >
      > I then get the following error messages:
      >
      > Traceback (most recent call last)
      > File "C:\Python24\te st\merge contract v1.py", in line3, in?
      > fileloader.file load(filename)
      > File ("C:\Python24\t ext\fileloader. py", in line2, in fileload
      > infile=open(fna me)
      > UnboundLocalErr or: local variable 'open' referenced before assignment
      > Script terminated
      >
      > Thanks for any help,
      > Luke
      >
      >
      >[/color]

      Comment

      • Luke

        #4
        Re: Beginner question: use function to read text file

        Thanks to both of you for the help, much appreciated!

        Luke


        Comment

        Working...