Determining the size of a file is a common task in many programming scenarios, such as validating uploads, managing disk space, or processing large datasets. Python provides several convenient ways to achieve this. This tutorial explores the most common and efficient methods for obtaining file sizes.
Using os.path.getsize()
The simplest and most direct approach is to use the os.path.getsize()
function from the os
module. This function directly returns the size of a file in bytes, given its path.
import os
file_path = 'my_document.txt' # Replace with your file path
try:
file_size = os.path.getsize(file_path)
print(f"The size of '{file_path}' is: {file_size} bytes")
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
except OSError as e:
print(f"An error occurred: {e}")
This method is generally preferred for its simplicity and readability. The try...except
block handles potential errors, such as the file not existing or permission issues.
Using os.stat()
The os.stat()
function provides more detailed information about a file, including its size, modification time, and permissions. To obtain the file size, you need to access the st_size
attribute of the returned stat
object.
import os
file_path = 'my_image.jpg'
try:
stat_info = os.stat(file_path)
file_size = stat_info.st_size
print(f"The size of '{file_path}' is: {file_size} bytes")
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
except OSError as e:
print(f"An error occurred: {e}")
While slightly more verbose than os.path.getsize()
, os.stat()
is useful when you need other file metadata as well.
Using pathlib
(Python 3.4+)
The pathlib
module provides an object-oriented way to interact with files and directories. It offers a more modern and often more readable alternative to the os
module.
from pathlib import Path
file_path = Path('my_data.csv')
try:
file_size = file_path.stat().st_size
print(f"The size of '{file_path}' is: {file_size} bytes")
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
except OSError as e:
print(f"An error occurred: {e}")
Using pathlib
can improve code clarity, especially in complex file system operations. The Path.stat()
method returns a stat
object, from which you can access the st_size
attribute.
Important Considerations
- Error Handling: Always include error handling (using
try...except
blocks) to gracefully handle cases where the file doesn’t exist or you don’t have the necessary permissions. - File Paths: Ensure that the file path you provide is correct and accessible. Relative paths are interpreted relative to the current working directory. Absolute paths specify the exact location of the file.
- Large Files: For extremely large files, consider reading the file in chunks to avoid loading the entire file into memory. However, if you only need the size, the methods described above are efficient and do not require reading the file contents.