Having problems with reading file in Python CGI

2006-06-26 Thread Kiana Toufighi
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Having problems with reading file in Python CGI

2006-06-26 Thread MooMaster
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

-- 
http://mail.python.org/mailman/listinfo/python-list