Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Having problems with reading file in Python CGI

Reply
Thread Tools

Having problems with reading file in Python CGI

 
 
Kiana Toufighi
Guest
Posts: n/a
 
      06-26-2006
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
 
Reply With Quote
 
 
 
 
MooMaster
Guest
Posts: n/a
 
      06-26-2006
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


 
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
Having Problems Reading RiData Compact Flash Card all of a sudden imagoogler@yahoo.com Digital Photography 7 11-12-2008 07:20 AM
CGI: triggering a file download & having a page appear Frank Reiff Ruby 1 07-31-2007 09:43 AM
Python-cgi or Perl-cgi script doubt praba kar Python 1 07-30-2005 08:25 AM
Python CGI - Accepting Input, Invoking Another Process, Ending CGI LarsenMTL Python 4 11-04-2004 05:59 PM
having serious problems getting a python CGI to work John Draper Python 3 06-02-2004 10:43 AM



Advertisments