473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.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
Jun 26 '06 #1
1 1274
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


Jun 26 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dennis Roberts | last post by:
I have a script to parse a dns querylog and generate some statistics. For a 750MB file a perl script using the same methods (splits) can parse the file in 3 minutes. My python script takes 25...
3
by: John Draper | last post by:
I am having a lot of problems trying to get a Python CGI to run. I have included 3 parts... 1) A simple stripped down python module. 2) An output of what I get when I do: python index.py...
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
2
by: Justin Lemkul | last post by:
Hello all, I am hoping someone out there will be able to help me. I am trying to install a program that utilizes NumPy. In installing NumPy, I realized that I was lacking Atlas. I ran into...
0
by: travis ray | last post by:
Hi, I have an extension in which a file object is created in python and passed down to a c extension which attempts to read from it or write to it. Writing to the file pointer seems to work...
1
by: khaleel.alyasini | last post by:
anyone could point me out where did i do wrong? it seems that i can't get back the original Lena image after the IDCT(inverse discrete cosine transform) process. the output raw image is nothing...
4
by: paduffy | last post by:
Folks, I've a Python 2.5 app running on 32 bit Win 2k SP4 (NTFS volume). Reading a file of 13 GBytes, one line at a time. It appears that, once the read line passes the 4 GByte boundary, I am...
12
by: xamdam | last post by:
Hi fellas, I am experiencing problems reading a 2GB zipfile consisting of multiple zipped files. I found a thread http://mail.python.org/pipermail/python-dev/2005-April/053027.html that mentions...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.