PYTHON

Binary Files


Binary file handling in Python allows you to work with non-text files, such as images, audio files, and other file formats that contain binary data. Unlike text files, binary files store data in a format that is not human-readable. Python provides built-in functions and methods to read from, write to, and manipulate binary files.

 

1. Opening a Binary File

To open a binary file in Python, use the open() function with the 'b' mode.

Syntax:

file_object = open(file_name, mode)
 

Common Modes:

  • 'rb': Read binary mode. Opens a file for reading in binary format.
  • 'wb': Write binary mode. Opens a file for writing in binary format (creates a new file or truncates an existing file).
  • 'ab': Append binary mode. Opens a file for appending in binary format (creates a new file if it doesn't exist).
  • 'r+b': Read and write binary mode. Opens a file for both reading and writing in binary format.
  • 'w+b': Write and read binary mode. Opens a file for both writing and reading in binary format (creates a new file or truncates an existing file).

Example:

# Open a file for reading in binary mode
file = open('example.bin', 'rb')
# Open a file for writing in binary mode
file = open('example.bin', 'wb')
# Open a file for appending in binary mode
file = open('example.bin', 'ab')
# Open a file for reading and writing in binary mode
file = open('example.bin', 'r+b')
 

 

2. Reading from a Binary File

There are several ways to read from a binary file in Python:

2.1 read()

Reads the entire file as a bytes object.

Example:

file = open('example.bin', 'rb')
content = file.read()
print(content)
file.close()
 

2.2 read(size)

Reads size bytes from the file.

Example:

file = open('example.bin', 'rb')
content = file.read(10)  # Read the first 10 bytes
print(content)
file.close()

 

2.3 readline()

Reads one line from the file. Binary files may not have lines, so this is less commonly used.

Example:

file = open('example.bin', 'rb')
line = file.readline()
print(line)
file.close()

 

2.4 readlines()

Reads all lines and returns them as a list of bytes objects. Again, this is less commonly used for binary files.

Example:

file = open('example.bin', 'rb')
lines = file.readlines()
for line in lines:
   print(line)
file.close()
 

 

3. Writing to a Binary File

You can write to a binary file using the write() and writelines() methods.

3.1 write()

Writes bytes to the file.

Example:

file = open('example.bin', 'wb')
file.write(b'\x00\x01\x02\x03\x04')
file.close()
 

3.2 writelines()

Writes a list of bytes objects to the file.

Example:

file = open('example.bin', 'wb')
lines = [b'\x00\x01\x02\x03\x04', b'\x05\x06\x07\x08\x09']
file.writelines(lines)
file.close()
 

 

4. Appending to a Binary File

Appending data to an existing file can be done using the append mode 'ab'.

Example:

file = open('example.bin', 'ab')
file.write(b'\x0A\x0B\x0C\x0D\x0E')
file.close()
 

 

5. Using the with Statement

Using the with statement for file handling ensures that the file is properly closed after its suite finishes, even if an exception is raised.

Example:

# Reading with `with` statement
with open('example.bin', 'rb') as file:
   content = file.read()
   print(content)
# Writing with `with` statement
with open('example.bin', 'wb') as file:
   file.write(b'\x00\x01\x02\x03\x04')
# Appending with `with` statement
with open('example.bin', 'ab') as file:
   file.write(b'\x0A\x0B\x0C\x0D\x0E')
 

 


PYTHON