python: writing binary file
November 20th, 2008 mysurface Posted in python | Hits: 153744 | 8 Comments »
Python is the best scripting language which I found it out perform Bash script as well as Lua. I like python’s scripting syntax, which is make sense and very convenient in string manipulations. Its now come to binary format manipulation, how convenient is python?
I have carry out some research on python into writing binary files. Either writing byte by byte manually, construct a binary stream then write it one short, or convert it from hex string, they are easy to write in python.
To open a file for binary writing is easy, it is the same way you do for reading, just change the mode into “wb”.
file = open("test.bin","wb")
But, how to write the binary byte into the file?
You may write it straight away with hex code like this:
file.write("\x5F\x9D\x3E")
file.close()
Now, check it out with hexedit,
hexedit test.bin
You will see this:
00000000 5F 9D 3E _.>
00000020
00000040
Now, open the file to append more bytes:
file = open("test.bin","ab")
What if I want to store by bin value into a stream and write it one short?
s ="\x45\xF3"
s = s + "%c%c" % (0x45,0xF3)
file.write(s)
file.close()
Any convenient ways if I can obtained a hex string, and want to convert it back to binary format?
Yes, you just need to import binascii
import binascii
hs="5B7F888489FEDA"
hb=binascii.a2b_hex(hs)
file.write(hb)
file.close()
Isn’t it easy? I love python. You may want to discover more functions of binascii with ipython.







January 6th, 2009 at 11:49 pm
this is why i love open source :D hope opensource become over developed that microsoft will also become open source lol anyway visit this website it also offers free download of open source scripts such as PHP, java etc…
http://www.zmartscript.co.cc
September 17th, 2009 at 6:14 pm
How do I manipulate bytes before writing to a file? Say I want to add a random number between 0 and 255.
Thanks
MNM
October 29th, 2009 at 2:38 pm
Thanks for this great article!
February 28th, 2010 at 7:54 am
Thanks! I was searching how to get images from a website, which haven’t got correct extensions… Using urllib, re and writing a binary file gives me quite useful effect. :)
June 21st, 2010 at 11:49 pm
THANK YOU.
I hope I can give back to the community by explain how this helped me with a problem. If this gets index by a search engine then someone will kill two birds with one stone.
HOW TO RECOVER WEBPAGE FROM BROWSER CACHE
I unwittingly lost an html template I was working on. I was able to recover the file from Google Chrome (you can also do this in Firefox in a similar way) by typing “about:cache” in the url. I then searched for the file; unfortunately the server gzips html to save bandwidth. All I had was rows of hex.
Thankful, I found this article and pieced together the file, uncompressed it, and successfully recovered my file. Phew!
August 18th, 2011 at 11:09 am
Yes.. This is a simple tutorial how to write binary file in python. thx for sharing
August 24th, 2011 at 11:59 am
Thanks a lot this really helps!!
August 24th, 2011 at 1:21 pm
Magnificent site. A lot of useful information here. I am sending it to several friends ans also sharing in delicious. And naturally, thanks in your effort!