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')

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?

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 ?

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.