Raimund Krämer
Raimund Krämer

Reputation: 1309

http file upload to disk

I am using an HTML input of type file to upload files to my own server. In the Python script on the server I can receive the file and its meta-data (like filename and type) as a FieldStorage object.

I have printed the fields of that object to the HTML page in order to see what they hold. When I let it print filename, type, and file (converted to a string), I get this:

Filename: Lighthouse.jpg Type: image/jpeg File: <open file '<fdopen>', mode 'w+b' at 0x04403578>

I assume that 0x04403578 is the address of the file in the server's memory. But what does mode 'w+b' mean? And how can I save that file to disk via Python if I have to support arbitrary filetypes?

Upvotes: 0

Views: 279

Answers (1)

tauroid
tauroid

Reputation: 46

File is just an open file object, like you would get from open. <fdopen> means it has been converted from a file descriptor. w+b means it is open for read and write in binary mode.

You can write it like a normal file i.e.

w = open('<filename>','w+b')
w.write(f.read())

Upvotes: 1

Related Questions