954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python can't handle large binary objects?

I'm having trouble writing binary data out to a file.

My end goal is to add a chunk to a PNG file; which has a format of: length (4 bytes), type (4 bytes), data (lots of bytes), crc32 (4 bytes). Collecting each of these pieces is not a problem, but when the chunk is inserted, each item gets a "\n" (0x0a) appended to it, and the numeric items (length, crc32) get written as strings instead of just their numeric values.

f = open('output.png', 'wb')
f.write(prependBytes)
f.write(fileLength)
f.write(pngChunk)
f.write(fileData)
f.write(fileCRC32)
f.write(appendBytes)
f.close()


How do I get the numeric values written to file as just their number, and how do I get Python to not append a \n to each item it writes?

mwjones
Newbie Poster
6 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

use chr function or struct module (docs.python.org/library/struct.html)

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

You should provide more information and example data. If you are working in python 3, a way to write a raw integer is f.write(bytearray((n,)). Otherwise what are the types of the values prependByte, fileLength, pngChunk, etc ?

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 
but when the chunk is inserted, each item gets a "\n"

Python does not write a "\n" to a file as it does with a print statement. If there is a "\n" it must be in the string, so print the string so you know what is being added. Also, ImageMagick will append two PNG files so use it if you can as it is tested.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: