Having problems with reading file in Python CGI

Discussion in 'Python' started by Kiana Toufighi, Jun 26, 2006.

  1. Hi,

    I have a simple CGI program that allows that user to upload a file.
    However, since accessing the the value of the uploaded file using the
    value attribute or the getvalue() method reads the entire file in memory
    as a string which is not what I want I'm making use of the file module.
    The problem is that all the my checks including "assert fileStream.file
    in not None" return true however readline() does not return the next
    line of the fileStream.file object! What am I doing wrong? Here's my code:

    Here's my code:

    caller:
    dataFile = form['data_file']
    fileproc = redrev.FileStreamProcessor()
    dataLines = fileproc.readStream(dataFile)

    callee:
    def readStream(self, fileStream):
    '''readStream: reads a stream of input returns a list of lines'''

    # list to hold data lines
    fileLines = []

    # make sure that the stream has been provided
    try:
    assert fileStream.file is not None
    except Exception, e:
    "No input to process"

    # use filestream object to get uploaded file's lines one by one
    if fileStream.file:
    while 1:
    line = fileStream.file.readline()
    if not line:
    break

    # process and store the line
    line.strip()
    fileLines.append(line)

    return fileLines


    Thanks,

    Kiana
    Kiana Toufighi, Jun 26, 2006
    #1
    1. Advertising

  2. Kiana Toufighi

    MooMaster Guest

    I haven't tried to use the CGI class for any CGI scripting, so I'm no
    expert...but I am familiar with file objects. You want to return the
    next line in the file object? Your loop will run until it hits the EOF,
    at which point it'll break...once you hit that, there is *no* next
    line.

    But it looks to me like you're trying to return the contents of the
    file in a list...is that the part that's not working? If so, you might
    wanna try this:

    def filereader(a_file):
    contents = []
    for line in a_file:
    contents.append(line.strip())
    print line
    print contents

    now if you create a file like so:
    myfile = file("Moo.txt", "r")
    filereader(myfile)

    You should see the contents of the file. So in your case, you should be
    able to change that horrible infinite while 1 loop with a break (which
    is straight from the python docs too... my programming languages
    teacher would have a fit!) into something like:

    for stuff in fileStream.file:

    Hope this helps!

    Kiana Toufighi wrote:
    > Hi,
    >
    > I have a simple CGI program that allows that user to upload a file.
    > However, since accessing the the value of the uploaded file using the
    > value attribute or the getvalue() method reads the entire file in memory
    > as a string which is not what I want I'm making use of the file module.
    > The problem is that all the my checks including "assert fileStream.file
    > in not None" return true however readline() does not return the next
    > line of the fileStream.file object! What am I doing wrong? Here's my code:
    >
    > Here's my code:
    >
    > caller:
    > dataFile = form['data_file']
    > fileproc = redrev.FileStreamProcessor()
    > dataLines = fileproc.readStream(dataFile)
    >
    > callee:
    > def readStream(self, fileStream):
    > '''readStream: reads a stream of input returns a list of lines'''
    >
    > # list to hold data lines
    > fileLines = []
    >
    > # make sure that the stream has been provided
    > try:
    > assert fileStream.file is not None
    > except Exception, e:
    > "No input to process"
    >
    > # use filestream object to get uploaded file's lines one by one
    > if fileStream.file:
    > while 1:
    > line = fileStream.file.readline()
    > if not line:
    > break
    >
    > # process and store the line
    > line.strip()
    > fileLines.append(line)
    >
    > return fileLines
    >
    >
    > Thanks,
    >
    > Kiana
    MooMaster, Jun 26, 2006
    #2
    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 Draper
    Replies:
    3
    Views:
    581
    Cameron Laird
    Jun 2, 2004
  2. Replies:
    7
    Views:
    1,028
    red floyd
    Jul 17, 2005
  3. spidey12345
    Replies:
    2
    Views:
    530
    ashani
    Oct 23, 2006
  4. Une bévue
    Replies:
    5
    Views:
    240
    Une bévue
    Jun 20, 2006
  5. Frank Reiff
    Replies:
    1
    Views:
    103
    Robert Klemme
    Jul 31, 2007
Loading...

Share This Page