python: writing binary file

November 20th, 2008 mysurface Posted in python | Hits: 206131 | 18 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:


Now, check it out with hexedit,

hexedit test.bin

You will see this:

00000000   5F 9D 3E                                                                                                _.>

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)

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

Isn’t it easy? I love python. You may want to discover more functions of binascii with ipython.

18 Responses to “python: writing binary file”

  1. 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…

  2. How do I manipulate bytes before writing to a file? Say I want to add a random number between 0 and 255.


  3. Thanks for this great article!

  4. 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. :)


    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.


    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!

  6. Yes.. This is a simple tutorial how to write binary file in python. thx for sharing

  7. Thanks a lot this really helps!!

  8. 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!

  9. 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.

  10. Great example, but how can I read the bytes.
    Some like this:

    how can I get this ‘ff’ after.

  11. I found a way to do what I say.
    It’s just this:


    print file(‘test.txt’,'rb’).read().encode(‘hex’)

    and the output is ‘ff’

  12. 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!

  13. 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.

  14. 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.

  15. Way cool! Some very valid points! I appreciate you writing this post plus the rest
    of the site is also really good.

  16. Thanks for this post. i want to made my own file fuzzer, this is very useful for me. thanks once again!

  17. The bonus offers delend on deposits and include luxury watches,
    laptops, phones, short camcorders, desktopss aand big wheel Club membership.
    Its amazing with the purpose of World Finance hold known our
    achievements thank you. Well in that case we would be returned nothing in
    most instances.

  18. Conmsult your physiccian and nutritionist before starting any diewt
    orr fast. In addition, you’ll need to use either of the eating plans
    for women taalked about above – as you might also be a
    workling mom with kids. Those who are striving
    to reach the same goals are often a rich resource for
    each other.

Leave a Reply