Python. Binary files. Examples of working with binary files




Binary files. Examples of working with binary files

This topic shows how you can save data in binary files without using the standard pickle or struct modules.


Contents


Search other websites:

1. The concept of binary files. Presentation of information in binary files

Python has tools for working with binary files. Binary files use strings of type bytes. This means when reading binary data from a file, an object of type bytes is returned.

The binary file is opened using the open() function, whose mode parameter contains the character ‘b’. More details about opening/closing binary files are described here.

Unlike text files, binary files do not convert characters to the end of the string ‘\n’.

An example demonstrating the features of the presentation of information in binary files.

# Python. Work with binary files

# Open binary file for reading
f = open('myfile1.bin', 'rb')

# Get a string from binary file
d = f.read()

# Display this string.
# The output will be as a string of characters
print("d = ", d) # d = b'\x80\x03]q\x00(K\x01\x88G@\x07\n=p\xa3\xd7\ne.'

# If print as a separate character,
# then the character code will be displayed - as an integer
print("d[5] = ", d[5]) # d[5] = 40
print("d[0] = ", d[0]) # d[0] = 128

# Use bin function for single character
print(bin(d[2])) # 0b1011101
f.close()

The result of the program

d = b'\x80\x03]q\x00(K\x01\x88G@\x07\n=p\xa3\xd7\ne.'
d[5] = 40
d[0] = 128
0b1011101

Based on the example above, the following conclusions can be drawn:

  • a string of binary data is output as a string;
  • a single character (element) of binary data is represented as 8-bit integers.

 

2. Writing / reading a list that contains real numbers. Example

 

# Binary files Writing / reading a list of real numbers

# 1. Given list of real numbers
L = [1.5, 2.8, 3.3]

# 2. File writing
# 2.1. Open file for writing
f = open('myfile3.bin', 'wb')

# 2.2. Bypass list and write data to a file
for item in L:
    # add the character '\ n' so that numbers can be recognized
    s = str(item) + '\n'

    # Encode () method - converts a string to a sequence of bytes
    bt = s.encode()
    f.write(bt) # write() method - write to file

# 2.3. Close file
f.close();

# 3. Read list from binary file 'myfile3.bin'
# 3.1. Create an empty list
L2 = []

# 3.2. Open file for reading
f = open('myfile3.bin', 'rb')

# 3.3. Bypass lines of a file, converting and adding to the list L2
for ln in f:
    x = float(ln) # get a number
    L2 = L2 + [x] # Add number to list

    # 3.4. Output the list
    print("L2 = ", L2) # L2 = [1.5, 2.8, 3.3]

# 3.5. Close file
f.close();

The result of the program

L2 = [1.5, 2.8, 3.3]

 

3. Writing/reading a tuple containing character strings. Example

In this example, the character strings in the binary file are separated by the character ‘\n’. Thus, it is possible to write and read information without losing its structure.

# Binary files. Writing / reading a tuple containing character strings

# 1. The specified tuple with strings
T = ( 'abc', 'def', 'ghi', 'jk lmn')

# 2. Write tuple T to file 'myfile5.bin'
# 2.1. Open file for writing
f = open('myfile5.bin', 'wb')

# 2.2. Tuple bypass cycle
for item in T:
    bt = (item + '\n').encode() # convert (str + '\n') => bytes
    f.write(bt) # write bt to file

# 2.3. Close file
f.close();

# 3. Read tuple from binary file 'myfile5.bin'
# 3.1. Open file for reading
f = open('myfile5.bin', 'rb')

# 3.2. A new tuple
T2 = ()

# 3.3. Read data from file
for line in f:
    s = line.decode() # convert bytes=>str
    s = s[:-1] # Remove the last character
    T2 = T2 + (s,) # Add string s to tuple

# 3.4. Print the tuple
print("T2 = ", T2)

# 3.5. Close file
f.close();

The result of the program

T2 = ('abc', 'def', 'ghi', 'jk lmn')

 



4. Writing / reading a set containing real numbers. Example

A set that contains only objects of the same type can be written to a file. In this example, many real numbers are written.

# Binary files. Writing/reading a set that contains real numbers

# 1. Given set
M = { 0.2, 0.3, 0.8, 1.2, 1.77 }

# 2. Writing the set M to the file 'myfile6.bin'
# 2.1. Open file for writing
f = open('myfile6.bin', 'wb')

# 2.2. The loop of bypass the set
for item in M:
    s = str(item) + '\n' # convert float=>str + '\n'
    bt = s.encode() # convert str=>bytes
    f.write(bt) # write bt to file

# 2.3. Close file
f.close();

# 3. Read set from binary file 'myfile6.bin'
# 3.1. Open file for reading
f = open('myfile6.bin', 'rb')

# 3.2. Create a new set
M2 = set()

# 3.3. Read data from file
for line in f:
    x = float(line) # convert bytes=>x
    M2 = M2.union({x}) # add x to the set

# 3.4. Print the set
print("M2 = ", M2)

# 3.5. Close file
f.close()

The result of the program

M2 = {0.2, 0.8, 0.3, 1.77, 1.2}

The content of myfile6.bin file

0.2
0.8
1.77
0.3
1.2

 

5. Writing/reading a two-dimensional matrix of rows of a given size. Example

In the example, the matrix is presented in the form of a list.

# Binary files. Writing/reading a matrix that contains rows

# 1. Given matrix of rows 3*4 in size

MATRIX = [ [ 'aa', 'ab', 'ac', 'ad'],
           [ 'ba', 'bb', 'bc', 'bd'],
           [ 'ca', 'cb', 'cc', 'cd'] ]

# 2. Writing MATRIX matrix to the file 'myfile7.bin'
# 2.1. Open file for writing
f = open('myfile7.bin', 'wb')

# 2.2. First, write the size of the matrix
m = 3
n = 4

# convert m, n to str type
sm = str(m) + '\n'
sn = str(n) + '\n'

# convert str to bytes
bm = sm.encode()
bn = sn.encode()

# write matrix sizes to file
f.write(bm)
f.write(bn)

# 2.3. The loop of matrix traversal
for row in MATRIX:
    # here you just need to write lines with the character '\n'
    for item in row:
        item = item + '\n'
        bt = item.encode() # str=>bytes
        f.write(bt) # write bt to file

# 2.3. Close file
f.close();

# 3. Read matrix from binary file 'myfile7.bin'
# 3.1. Open file for reading
f = open('myfile7.bin', 'rb')

# 3.2. A new matrix
MATRIX2 = []

# 3.3. Read data from file
# 3.3.1. First, read size
s = f.readline()
m2 = int(s)
s = f.readline()
n2 = int(s)

# 3.3.2. Loop of reading the lines and the creation of a matrix size m2*n2
i = 0
while i < m2: # m2 rows in matrix
    row = [] # one row in list
    j = 0
    while j < n2:
        bs = f.readline() # read one element of type bytes
        s = bs.decode() # convert bytes=>str
        s = s[:-1] # remove '\n'
        row += [s] # add to the list
        j = j+1
    MATRIX2 += [row] # add one row of the list to the matrix
    i = i+1

# 3.4. Display new matrix
i = 0
while i < m2:
    print("MATRIX2[", i, "] = ", MATRIX2[i])
    i = i+1

# 3.5. Close file
f.close()

The result of the program

MATRIX2[ 0 ] = ['aa', 'ab', 'ac', 'ad']
MATRIX2[ 1 ] = ['ba', 'bb', 'bc', 'bd']
MATRIX2[ 2 ] = ['ca', 'cb', 'cc', 'cd']

The content of file myfile7.txt

3
4
aa
ab
ac
ad
ba
bb
bc
bd
ca
cb
cc
cd

 

6. Writing/reading a dictionary. Example

Let some dictionary be given that needs to be written to a binary file.

# Binary files. Writing/reading a dictionary

# 1. The specified dictionary. Pairs of type str:int
D = { 'One':1, 'Two':2, 'Three':3, 'Four':4 }

# 2. Writing D dictionary to file 'myfile8.bin'
# 2.1. Open file for writing
f = open('myfile8.bin', 'wb')

for key in D: # dictionary bypass
    # get the value
    value = D[key]

    # Write sequentially key, then value
    svalue = str(value) + '\n' # convert value to string
    skey = key + '\n' # add '\n' to key string

    # Convert key:svalue from string to bytes
    b_key = skey.encode()
    b_svalue = svalue.encode()

    # write b_key, b_svalue to the file
    f.write(b_key)
    f.write(b_svalue)

# 2.3. Close file
f.close();

# 3. Read dictionary from binary file 'myfile8.bin'
# 3.1. Open file for reading
f = open('myfile8.bin', 'rb')

# 3.2. New dictionary to be read from file
D2 = dict()

# 3.3. Read the whole file at once
b_strings = f.readlines() # b_strings - list of strings of type bytes

# 3.4. Bypass the b_strings list.
# The key is read first, then the value, etc.
fkey = True # if True, then the key is read, otherwise the value is read
for item in b_strings:
    if fkey: # check if key is read
        skey = item.decode() # convert bytes=>str
        key = skey[:-1] # remove '\n'
        fkey = False
    else:
        svalue = item.decode() # convert bytes=>str
        value = int(svalue) # convert str=>int
        D2[key] = value # add to dictionary
        fkey = True # indicate that the next iteration will be the key

# 3.5. Output the dictionary
print("D2 = ", D2)

# 3.6. Close file
f.close()

The result of the program

D2 = {'One': 1, 'Two': 2, 'Three': 3, 'Four': 4}

The content of myfile8.txt file

One
1
Two
2
Three
3
Four
4

 

7. Copying one binary file to another

 

# Binary files. Copying files

# 1. Open the files
f1 = open('myfile8.bin', 'rb') # file - source, it is opened for reading
f2 = open('copyfile8.bin', 'wb') # file - copy

# 2. Read file f1 to bstrings string list
bstrings = f1.readlines()

# 3. Write bstrings string list to f2 file
f2.writelines(bstrings)

# 4. Close the files
f1.close()
f2.close()

 

8. Combining two binary files. Example

The example implements the operation of combining two files into the resulting third file. First, data from source files is read into lists. Then, these lists are concatenated and the resulting list is written to the result file.

# Combining files myfile1.bin+myfile2.bin => myfile3.bin
# 1. Open files to read
f1 = open('myfile1.bin', 'rb')
f2 = open('myfile2.bin', 'rb')

# 2. Read files into L1, L2 lists
L1 = f1.readlines()
L2 = f2.readlines()

# 3. Combine lists
L3 = L1 + L2

# 4. Close files myfile1.bin, myfile2.bin
f1.close()
f2.close()

# 5. Open file myfile3.bin to write
f3 = open('myfile3.bin', 'wb')

# 6. Write list L3 into the file
f3.writelines(L3)

# 7. Close the result file
f3.close()

 


Related topics