python: writing binary file
November 20th, 2008 mysurface Posted in python | Hits: 192075 | 15 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!
April 1st, 2012 at 2:18 am
I do accept as true with all the concepts you have presented on your post. They’re very convincing and will definitely work. Still, the posts are too brief for beginners. May you please lengthen them a little from subsequent time? Thank you for the post.
June 23rd, 2012 at 1:02 am
Great example, but how can I read the bytes.
Some like this:
file(‘test.txt’,'wb’).write(‘\xff’)
how can I get this ‘ff’ after.
June 23rd, 2012 at 2:50 am
I found a way to do what I say.
It’s just this:
f=file(‘test.txt’,'wb’)
f.write(‘\xff’)
f.close()
print file(‘test.txt’,'rb’).read().encode(‘hex’)
and the output is ‘ff’
February 13th, 2013 at 6:13 am
Greetings from Florida! I’m bored to death at work so I decided to browse your website on my iphone during lunch break. I enjoy the info you present here and can’t wait to take a look
when I get home. I’m amazed at how quick your blog loaded on my mobile .. I’m not even
using WIFI, just 3G .. Anyways, fantastic site!
April 10th, 2013 at 12:11 pm
Pretty beneficial post. I just stumbled upon your blog and wanted to say that I have extremely enjoyed reading your blog posts. Any way I’ll be subscribing for your feed and I hope you article once more soon.
April 24th, 2013 at 5:19 am
Heya i am for the first time here. I came across this board and I
find It really helpful & it helped me out a lot. I hope to present one thing back and help others such as you helped me.
June 2nd, 2013 at 7:04 am
Way cool! Some very valid points! I appreciate you writing this post plus the rest
of the site is also really good.