How Python reads and writes binary files

Source: Internet
Author: User
Tags unpack hex code
The examples in this article describe how Python reads and writes binary files. Share to everyone for your reference. Specific as follows:

Beginner python, now to read a binary file, find doc only found that file provides a read and write function, and read and write is a string, if just read and write char, such as a byte of the line, to read and write such as int,double, such as multi-byte data is inconvenient. A post was found online, using the pack and unpack functions inside the struct module to read and write. Write your own code below to verify it.

>>> from struct import *>>> file = open (r "C:/debug.txt", "WB") >>> File.write (Pack ("Idh", 12345, 67.89, ()) >>> File.close ()

And then read it in.

>>> file = open (r "C:/debug.txt", "RB") >>> (a,b,c) = Unpack ("Idh", File.read (8+8+2)) >>> a,b,c (12345, 67.890000000000001, i) >>> print a,b,c12345 67.89 15>>> file.close ()

The size of the data that you need to be aware of during operation

Pay attention to WB,RB in the B word, must not be less

Method 1:

Myfile=open (' c:\\t ', ' RB ') S=myfile.read (1) byte=ord (s) #将一个字节 read into a number of print hex (byte) #转换成16进制的字符串

Method 2

Import structmyfile=open (' c:\\t ', ' RB '). Read (1) Print struct.unpack (' C ', myfile) print struct.unpack (' B ', myfile)

Write

To open a file for binary writing are easy, it's the same-the-mode you do for reading, and just change the "WB".
File = Open ("Test.bin", "WB")
But what do you write the binary byte into the file?
You could 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'll 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 ()

Hopefully this article will help you with Python programming.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.