Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upload file with Python Mechanize

When I run the following script:

from mechanize import Browser
br = Browser()
br.open(url)
br.select_form(name="edit_form")
br['file'] = 'file.txt'
br.submit()

I get: ValueError: value attribute is readonly

And I still get the same error when I add:

br.form.set_all_readonly(False)

So, how can I use Python Mechanize to interact with a HTML form to upload a file?

Richard

like image 316
hoju Avatar asked Aug 19 '09 13:08

hoju


1 Answers

This is how to do it properly with Mechanize:

br.form.add_file(open(filename), 'text/plain', filename)
like image 95
hoju Avatar answered Oct 11 '22 07:10

hoju