How Python reads and writes binary files

Source: Internet
Author: User
Tags pack unpack hex code

The example in this article describes how Python reads and writes binary files. Share to everyone for your reference. Specifically as follows:

Beginners Python, now read a binary file, find doc only found that file provides a read and write functions, and read and write is a string, if only read and write char and so on a byte of the row, to read and write such as int,double data such as multibyte is inconvenient. Find a post on the Internet, use the pack and unpack function inside the struct module to read and write. Write the code to verify the following.

?

1 2 3 4 >>> from struct import * >>> file = open (r "C:/debug.txt", "WB") >>> File.write (Pack ("IDH", 1234 5, 67.89) >>> file.close ()

And then read it in.

?

1 2 3 4 5 6 7 >>> file = open (r "C:/debug.txt", "RB") >>> (a,b,c) = Unpack ("Idh", File.read (8+8+2)) >>> a,b,c (12345, 67.890000000000001) >>> print a,b,c 12345 67.89 >>> file.close ()

You need to be aware of the size of the data during the operation

Note that the B word in wb,rb, must not be less

Method 1:

?

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

Method 2

?

1 2 3 4 Import struct Myfile=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 and it is the same way your do for reading and just change the ' mode into ' WB '.

File = Open ("Test.bin", "WB")

But, how do I write the binary byte into the file?

You could write it straight away with hex code like this:

File.write ("x5fx9dx3e") File.close ()

Now, the check it out with HexEdit,

HexEdit Test.bin

You'll be here:

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 = "X45xf3" 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, just need to import binascii

Import binascii hs= "5b7f888489feda" Hb=binascii.a2b_hex (HS) File.write (HB) File.close ()

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.