💾 Handling Binary Files
Binary files contain non-text data like images, executables, or compressed data. Python provides tools to read, write, and manipulate binary files safely and efficiently.
# Basic binary file operations
# Read binary file
with open('image.jpg', 'rb') as file:
data = file.read()
print(f"File size: {len(data)} bytes")
# Write binary file
with open('copy.jpg', 'wb') as file:
file.write(data)
🎯 Binary File Modes
Python offers specific modes for binary file operations.
Reading Binary Files
import os
# Reading binary files in chunks
def read_binary_file(filename, chunk_size=1024):
"""Read binary file in chunks"""
try:
with open(filename, 'rb') as file:
while True:
chunk = file.read(chunk_size)
if not chunk:
break
print(f"Read chunk of {len(chunk)} bytes")
# Process chunk here
except FileNotFoundError:
print(f"File {filename} not found")
# Example usage
read_binary_file('example.bin')
Copying Binary Files
# Copy binary file safely
def copy_binary_file(source, destination):
"""Copy binary file safely"""
try:
with open(source, 'rb') as src:
with open(destination, 'wb') as dst:
while True:
chunk = src.read(8192) # 8KB chunks
if not chunk:
break
dst.write(chunk)
print(f"Copied {source} to {destination}")
return True
except Exception as e:
print(f"Copy failed: {e}")
return False
# Example usage
result = copy_binary_file('input.jpg', 'output.jpg')
print(f"Copy successful: {result}")
Structured Binary Data
import struct
# Working with structured binary data
def write_binary_data():
"""Write structured binary data"""
# Pack data into binary format
# Format: i = integer, f = float, 10s = 10-character string
data = struct.pack('if10s', 42, 3.14, b'Hello')
with open('data.bin', 'wb') as file:
file.write(data)
print(f"Written {len(data)} bytes")
def read_binary_data():
"""Read structured binary data"""
try:
with open('data.bin', 'rb') as file:
data = file.read()
# Unpack binary data
integer, float_val, string_val = struct.unpack('if10s', data)
print(f"Integer: {integer}")
print(f"Float: {float_val}")
print(f"String: {string_val.strip(b'\\x00').decode()}")
except FileNotFoundError:
print("Binary data file not found")
write_binary_data()
read_binary_data()
File Information
# File size and metadata
def get_file_info(filename):
"""Get binary file information"""
try:
stat = os.stat(filename)
return {
'size': stat.st_size,
'created': stat.st_ctime,
'modified': stat.st_mtime,
'readable': os.access(filename, os.R_OK),
'writable': os.access(filename, os.W_OK)
}
except Exception as e:
print(f"Error getting file info: {e}")
return None
info = get_file_info('data.bin')
if info:
print(f"File size: {info['size']} bytes")
print(f"Readable: {info['readable']}")
🔍 Advanced Binary Operations
Python allows sophisticated binary file manipulation.
File Integrity Checking
import hashlib
# File integrity checking
def calculate_checksum(filename, algorithm='md5'):
"""Calculate file checksum for integrity"""
try:
hash_func = hashlib.new(algorithm)
with open(filename, 'rb') as file:
while True:
chunk = file.read(8192)
if not chunk:
break
hash_func.update(chunk)
return hash_func.hexdigest()
except FileNotFoundError:
return None
# Example checksum calculation
checksum = calculate_checksum('data.bin')
if checksum:
print(f"MD5 checksum: {checksum}")
Object Serialization
import pickle
# Working with Python objects (serialization)
def save_object(obj, filename):
"""Save Python object to binary file"""
try:
with open(filename, 'wb') as file:
pickle.dump(obj, file)
return True
except Exception as e:
print(f"Error saving object: {e}")
return False
def load_object(filename):
"""Load Python object from binary file"""
try:
with open(filename, 'rb') as file:
return pickle.load(file)
except Exception as e:
print(f"Error loading object: {e}")
return None
# Example with complex object
data_object = {
'numbers': [1, 2, 3, 4, 5],
'text': 'Hello, World!',
'nested': {'key': 'value'}
}
save_object(data_object, 'object.pickle')
loaded_object = load_object('object.pickle')
print(f"Loaded object: {loaded_object}")
File Comparison
# Binary file comparison
def compare_binary_files(file1, file2):
"""Compare two binary files"""
try:
with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
while True:
chunk1 = f1.read(8192)
chunk2 = f2.read(8192)
if chunk1 != chunk2:
return False
if not chunk1: # End of both files
return True
except Exception as e:
print(f"Comparison error: {e}")
return False
# Example comparison
are_same = compare_binary_files('file1.bin', 'file2.bin')
print(f"Files are identical: {are_same}")
Binary File Handler Class
from pathlib import Path
# Safe binary file operations
class BinaryFileHandler:
def __init__(self, filename):
self.filename = Path(filename)
def read_all(self):
"""Read entire binary file"""
try:
with open(self.filename, 'rb') as file:
return file.read()
except Exception as e:
print(f"Read error: {e}")
return None
def write_all(self, data):
"""Write data to binary file"""
try:
# Create directory if needed
self.filename.parent.mkdir(parents=True, exist_ok=True)
with open(self.filename, 'wb') as file:
file.write(data)
return True
except Exception as e:
print(f"Write error: {e}")
return False
# Example usage
handler = BinaryFileHandler('binary_data.bin')
handler.write_all(b'Hello, binary world!')
content = handler.read_all()
print(f"File content: {content}")
📋 Binary Operations Reference Table
Operation | Method | Use Case |
---|---|---|
Read all | file.read() | Small files |
Read chunk | file.read(size) | Large files |
Write all | file.write(data) | Complete replacement |
Append | Mode 'ab' | Add to existing |
Seek | file.seek(pos) | Random access |
Tell | file.tell() | Current position |
🎯 Key Takeaways
Was this helpful?
Track Your Learning Progress
Sign in to bookmark tutorials and keep track of your learning journey.
Your progress is saved automatically as you read.