1

I've got a program that reads and sorts information from a CSV file but I can't find out how to upload a file and either read it directly (which I don't think is possible) or upload it to a server.

Everything I try to Google either doesn't work or seems too ambiguous.

Has anyone got any idea how to upload the file from an HTML form so that I can read it in the program?

To read the file I'm using the csv module:

readerCTR = csv.reader(open("/home/ctrdata.csv", "rb"))

I'm using a really basic html form:

<form action="test" method="post" enctype="multipart/form-data">
Upload file: <input type="file" name="myfile" /> <br />
         <input type="submit" name="submit" value="Submit" />
</form>

and I've been trying to use the tutorial on CGI at docs.python

form = cgi.FieldStorage()
fileitem = form["myfile"]
if fileitem.file:
    linecount = 0
    while 1:
        line = fileitem.file.readline()
        if not line: break
        linecount = linecount + 1

but I just get key errors.

KeyError: 'myfile'

It seems like it isn't getting passed through at all. If I check the debugger:

>>> form
FieldStorage(None, None, [])

None of this makes ANY sense to me. I've never had to upload files before. I do have a server that I could save it to if I need to, but it would be ideal if I could just read it and temporarily save the data.

Do you think it could be that I'm using Firefox and Linux?

3
  • Silly question, but do you have a closing </form> tag? If not, perhaps the browser is closing it automatically, so that the file input isn't contained in the form itself? Commented Nov 23, 2011 at 17:39
  • This should work. As Daniel suggested, check your form. Commented Nov 23, 2011 at 22:20
  • Sorry, yes the form does have a closing tag I was just in a rush to post it. I cannot figure out why I am getting this key error Commented Nov 24, 2011 at 9:04

2 Answers 2

1

The problem with this was that I was using Pylons, so it was almost ignoring the CGI.

So instead I should have been using:

request.POST['myfile'].value
Sign up to request clarification or add additional context in comments.

Comments

0

That you get the error means the upload works - sort of: Your CGI script is called but there is something wrong with the parameters. Steps to debug this:

  1. Print the form.keys() to sys.stdout. That should give you a list of keys so you can check for typos.

  2. Check the version of your cgi module. The documentation says

    and the latest addition is support for file uploads from a form

(my emphasis). Maybe your version is too old.

2 Comments

Im using Python 2.6 so I think thats what version the cgi will be. If I try TypeError: sys.stdout(form.keys()) I just get 'file' object is not callable which doesnt make sense as it works fine with print. Although if I do print it it shows FieldStorage(None, None, [])
Sorry. Try print form.keys() or sys.stdout.**write**() :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.