Complete guide to Python's os.truncate function covering file size modification, truncation examples, and practical usage.
Last modified April 11, 2025
This comprehensive guide explores Python’s os.truncate function, which modifies file size by truncating or extending it. We’ll cover usage scenarios, permission requirements, and practical examples.
The os.truncate function changes the size of a file to the specified length. If the file is extended, new bytes are filled with zeros.
Key parameters: path (file to modify), length (new size in bytes). Requires write permission on the file. Works on regular files, not directories.
The most common use of os.truncate is reducing file size by discarding content beyond the specified length. This example demonstrates basic truncation.
basic_truncate.py
import os
file_path = “data.txt” with open(file_path, “w”) as f: f.write(“This is some sample text for truncation demonstration.”)
original_size = os.path.getsize(file_path) print(f"Original size: {original_size} bytes")
os.truncate(file_path, 20) new_size = os.path.getsize(file_path) print(f"New size: {new_size} bytes")
with open(file_path) as f: print(f"Content: ‘{f.read()}’")
This creates a file, then truncates it to 20 bytes. The remaining content will be the first 20 bytes of the original text.
Note that truncation happens immediately at the filesystem level, without needing to reopen the file.
When specifying a length larger than the current file size, os.truncate extends the file by padding with null bytes. This example shows file extension.
extend_file.py
import os
file_path = “empty.dat”
with open(file_path, “w”): pass
os.truncate(file_path, 1024) size = os.path.getsize(file_path) print(f"File size after extension: {size} bytes")
with open(file_path, “rb”) as f: content = f.read(16) # Read first 16 bytes print(f"First 16 bytes: {content}")
This creates an empty file, then extends it to 1024 bytes. The new space is filled with zeros. Binary mode is used to read the null bytes.
File extension is useful for preallocating space or creating sparse files.
os.ftruncate works similarly but operates on an open file descriptor instead of a path. This example shows both functions for comparison.
fd_truncate.py
import os
file_path = “log.txt”
with open(file_path, “w”) as f: f.write(“Initial content for truncation test”)
os.truncate(file_path, 10) print(f"After path truncate: {os.path.getsize(file_path)} bytes")
with open(file_path, “r+”) as f: fd = f.fileno() os.ftruncate(fd, 5) print(f"After fd truncate: {os.path.getsize(file_path)} bytes")
Both functions achieve the same result but take different parameters. os.ftruncate requires an open file descriptor.
The file must be opened in a mode that allows writing for os.ftruncate.
os.truncate can raise various exceptions. This example demonstrates proper error handling for common scenarios.
error_handling.py
import os import errno
file_path = “protected.txt”
try: # Attempt to truncate a non-existent file os.truncate(“nonexistent.txt”, 100) except FileNotFoundError: print(“Error: File not found”)
try: # Attempt to truncate without permissions os.truncate("/root/protected.txt", 100) except PermissionError: print(“Error: Permission denied”)
try: # Attempt to truncate a directory os.truncate("/tmp", 100) except IsADirectoryError: print(“Error: Cannot truncate a directory”)
try: # Invalid length os.truncate(file_path, -1) except OSError as e: if e.errno == errno.EINVAL: print(“Error: Invalid length specified”)
This shows handling for file not found, permission issues, directory truncation, and invalid length parameters. Each case raises a different exception.
Always validate inputs and handle potential errors when working with filesystem operations.
os.truncate can be combined with other file operations for more complex workflows. This example shows integration with file reading.
combined_operations.py
import os
file_path = “data.bin”
with open(file_path, “wb”) as f: f.write(b"\x01\x02\x03\x04\x05\x06\x07\x08")
os.truncate(file_path, 4)
with open(file_path, “rb”) as f: content = f.read() print(f"Remaining bytes: {content}") print(f"Size after truncate: {len(content)} bytes")
os.truncate(file_path, 8) with open(file_path, “rb”) as f: content = f.read() print(f"Extended bytes: {content}") print(f"Size after extension: {len(content)} bytes")
This creates a binary file, truncates it, then extends it while verifying the content at each step. The null bytes added during extension are visible.
Binary mode is essential when working with non-text data or examining the actual bytes added during extension.
This example benchmarks os.truncate against alternative methods of file size modification to demonstrate performance characteristics.
performance_test.py
import os import timeit
file_path = “large_file.bin” size_mb = 10 # 10MB file test_size = 5 # Truncate to 5MB
def create_large_file(): with open(file_path, “wb”) as f: f.seek(size_mb * 1024 * 1024 - 1) f.write(b"\0")
def method_truncate(): os.truncate(file_path, test_size * 1024 * 1024)
def method_write(): with open(file_path, “r+”) as f: f.truncate(test_size * 1024 * 1024)
create_large_file()
truncate_time = timeit.timeit(method_truncate, number=100) write_time = timeit.timeit(method_write, number=100)
print(f"os.truncate average: {truncate_time/100:.6f}s") print(f"file.truncate average: {write_time/100:.6f}s")
os.remove(file_path)
This creates a large file, then compares truncation methods. os.truncate is generally faster as it operates at the filesystem level.
The performance difference becomes more significant with larger files and frequent truncation operations.
Permission requirements: Write permission on the file is required
Data loss: Truncation permanently removes file content
Race conditions: File may change between size check and truncation
Symbolic links: Follows symlinks by default (use os.path.realpath)
Cross-platform: Behavior consistent across Unix and Windows
Backup important data: Before performing destructive operations
Check file type: Verify it’s a regular file before truncating
Handle errors: Implement proper exception handling
Document size changes: Log truncation operations for audit
Consider alternatives: For some cases, file.truncate() may be better
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.