Python to read and write binary files,

Source: Internet
Author: User
Tags hex code

Python to read and write binary files,

This example describes how to read and write binary files in python. Share it with you for your reference. The details are as follows:

Python beginners, now want to read a binary file, find doc only found that file provides a read and write functions, and read and write are strings, if only read and write char and other bytes are okay, it is inconvenient to read and write data in multiple data segments, such as int and double. Find a post on the Internet and use the pack and unpack functions in the struct module to read and write data. Write your own code to verify it.

>>> from struct import *>>> file = open(r"c:/debug.txt", "wb")>>> file.write(pack("idh", 12345, 67.89, 15))>>> file.close()

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, 15)>>> print a,b,c12345 67.89 15>>> file.close()

Pay attention to the data size during the operation.

Note that B characters in wb and rb must be less

Method 1:

Myfile = open ('C: \ t', 'rb') s = myfile. read (1) byte = ord (s) # read a byte into a number of print hex (byte) # convert it to a hexadecimal string

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 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" % (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 ()

I hope 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.