Determining File Size in Python
Measuring the size of a file is a common task in programming. Python provides a straightforward method for accomplishing this through its os.path module.
Solution:
To check the size of a file in Python, you can utilize the os.path.getsize() function. This function takes a file path as an argument and returns the file's size in bytes.
Implementation:
The following code snippet demonstrates how to use os.path.getsize() to retrieve a file's size:
import os # Provide the path to the file whose size you want to determine. file_path = "/path/to/file.mp3" try: # Use os.path.getsize() to obtain the file size. file_size = os.path.getsize(file_path) print("File size:", file_size, "bytes") except FileNotFoundError: print("Error: File not found.")
Additional Points:
The above is the detailed content of How Can I Determine a File\'s Size in Python?. For more information, please follow other related articles on the PHP Chinese website!