views:

58

answers:

1

I am trying to create a binary file in Python3 but i don 't know how to save it, the manual doesn 't say anything about that.

i create a code but i don 't catch the mistake:

s = open('/home/hidura/test.jpeg', 'wb')
s.write(str.encode(formFields[5]))
s.close()
+2  A: 

If you want to convert a string to a bytestring in using a specific encoding (for example UTF-8) then use this:

formFields[5].encode('utf8')

I'd also advise you to use the with statement instead of closing the file manually:

with open('/home/hidura/test.jpeg', 'wb') as s:
    s.write(...)
Mark Byers
I did but the mistake is: Error interpreting JPEG image file (Not a JPEG file: starts with 0x5c 0x6e)I think the problem is maybe in the binary code here is:\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x01\\x00H\\x00H\\x00\\x00\\xff\\xdb\\x00C\\x00\\x05\\x03\\x04\\x04\\x04\\x03\\x05\\x04\\x04\\x04\\x05\\x05\\x05\\x06\\x07\\x0c\\x08\\x07\\x07\\x07\\x07\\x0f\\x0b\\x0b\\t\\x0c\\x11\\x0f\\x12\\x12\\x11\\x0f\\x11\\x11\\x13\\x16\\x1c\\x17\\x13\\x14\\x1a\\x15\\x11\\x11\\x18!\\x18\\x1a\\x1d\\x1d\\x1f\\x1f\\x1f\\x13\\x17"$"\\x1e$\\x1c
hidura
@hidura: I think the problem is that your string is wrong. It shouldn't be `\xff\\xd8...`, it should be `\xff\xd8...`.
Mark Byers
Yes there was the problem i fixed and finally the system works!!!
hidura