Having problems with reading file in Python CGI

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

    #1

    Having problems with reading file in Python CGI

    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.FileStre amProcessor()
    dataLines = fileproc.readSt ream(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.appen d(line)

    return fileLines


    Thanks,

    Kiana
  • MooMaster

    #2
    Re: Having problems with reading file in Python CGI

    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_fi le):
    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(myfi le)

    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:[color=blue]
    > 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.FileStre amProcessor()
    > dataLines = fileproc.readSt ream(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.appen d(line)
    >
    > return fileLines
    >
    >
    > Thanks,
    >
    > Kiana[/color]

    Comment

    Working...