Saturday, 7 September 2013

open('file_path').read() - save contents back to file

open('file_path').read() - save contents back to file

I need to upload a file to the server using urllib2. Since I cannot use
any external libraries (like requests and others) because I am using
OpenOffice python, I needed a simple way to post file data.
So I came with:
post_url =
"http://localhost:8000/admin/oo_file_uploader?user_id=%s&file_id=%s" %
(user_id, file_id)
file_path = doc.Location.replace('file://', '')
data = {"file": open(file_path).read()}
urllib2.urlopen(post_url, data)
which posts something to the server.
I wonder if it is possible to save posted contents back to the file using
python?

No comments:

Post a Comment