- Understand binary data and file formats
- Read and write binary files using Python
- Use struct module for binary data manipulation
- Handle common binary file types and operations
Working with Binary Files
Understanding Binary Data
While text files contain human-readable characters, binary files store data in a format that computers understand directly. Think of text as written words and binary as the raw information that makes up images, sounds, or program executables.
Opening Binary Files
Use the 'b' flag with open() to work with binary data.
# Open binary file for reading
with open('image.jpg', 'rb') as file:
data = file.read()
# Open binary file for writing
with open('output.bin', 'wb') as file:
file.write(binary_data)
Reading Binary Data
Reading Bytes
with open('file.bin', 'rb') as file:
# Read all bytes
all_bytes = file.read()
# Read specific number of bytes
first_10_bytes = file.read(10)
# Read one byte at a time
byte = file.read(1)
Using bytearray
with open('data.bin', 'rb') as file:
buffer = bytearray(file.read())
# Now you can modify the buffer
buffer[0] = 255
Writing Binary Data
Writing Bytes
data = b'\x00\x01\x02\x03'
with open('output.bin', 'wb') as file:
file.write(data)
Writing from bytearray
buffer = bytearray([1, 2, 3, 4, 5])
with open('numbers.bin', 'wb') as file:
file.write(buffer)
Working with Struct Module
The struct module helps convert between Python values and C-style binary data.
import struct
# Pack data into binary format
packed_data = struct.pack('i', 42) # 'i' for integer
# Unpack binary data
unpacked_value = struct.unpack('i', packed_data)[0]
Common Binary File Types
Images
# Reading image dimensions (simplified example)
with open('image.png', 'rb') as file:
header = file.read(8)
# Parse PNG header to get dimensions
Audio Files
# Reading WAV file header
with open('sound.wav', 'rb') as file:
riff_header = file.read(4) # Should be 'RIFF'
file_size = struct.unpack('<I', file.read(4))[0]
Processing Image Files Example
Imagine processing a database backup file. You might need to:
- Read binary headers to understand file structure
- Extract specific data records
- Modify certain fields
- Write the updated data back to a new file
Best Practices
- Always use binary mode ('b') for binary files
- Be careful with endianness (byte order)
- Use struct module for complex binary formats
- Handle buffer sizes appropriately for large files
- Validate data integrity when reading
Binary file handling opens up possibilities for working with multimedia, compressed data, and specialized file formats. It's like learning to read the computer's native language.
