Examples
- How to Create Directory in Python? New and If Not Exists
- How to Change Directory in Python? Change Current Working Directory
- How to Get Current Working Directory in Python?
- How to Remove Directory in Python? (Empty & With Files)
- How to Copy Directory in Python? Program With Examples
- How to Open Directory in Python? Program & Example
- How to Read All Files in Directory in Python? (All Types of Files)
- How to Copy File in Python From One Directory to Another? (With and Without shutil)
- Python File Append Program (Append to File)
- How to Read Text File in Python Line by Line? (3 Ways)
- How to Get File Name From File Path in Python?
- Get Number of Lines in File in Python (Line Count)
- How to Get Size of File in Python? (File Size Program)
How to Get Size of File in Python? (File Size Program)
Finding the file size in Python is a crucial aspect when working with files. Whether you're managing storage space, assessing resource utilization, or validating file integrity, knowing the size of a file is essential.
In this post, we will learn how to check the file size in Python, enabling you to efficiently gather information about the size of any file.
Python provides several methods and techniques to find the file size, making it easy to work with files and extract valuable statistics. By using these methods, you can retrieve the size of a file in bytes, kilobytes (kb), megabytes (mb), or any other desired unit, depending on your specific requirements.
Get File Size in Python (in Bytes)
To get the size of a file in Python, you can use the os module. The following program will show the Python file size in bytes:
Code
import os
def get_file_size(file_path):
try:
size = os.path.getsize(file_path)
return size
except OSError:
print("File not found or inaccessible.")
return None
# Example usage
file_path = "path/to/your/file.ext"
file_size = get_file_size(file_path)
if file_size is not None:
print(f"The size of '{file_path}' is {file_size} bytes.")
Output
Depends on file
Explanation
In this example, the get_file_size function takes a file_path parameter and uses the os.path.getsize() function to get the size of file in bytes. If the file is not found or inaccessible, an OSError is raised, and the function returns None.
You can replace "path/to/your/file.ext" with the actual path to the file you want to get the size of.
Get File Size in MB (Mega Bytes)
In this version of the code, you can get the file size in megabytes (MB):
Code
import os
def get_file_size(file_path):
try:
size = os.path.getsize(file_path)
return size
except OSError:
print("File not found or inaccessible.")
return None
def convert_size_to_mb(size_bytes):
# Convert bytes to megabytes (MB)
size_mb = size_bytes / (1024 * 1024)
# Format the size with up to two decimal places
size_mb = "{:.2f}".format(size_mb)
return size_mb
# Example usage
file_path = "path/to/your/file.ext"
file_size = get_file_size(file_path)
if file_size is not None:
size_in_mb = convert_size_to_mb(file_size)
print(f"The size of '{file_path}' is {size_in_mb} MB.")
Output
Depends on file
Explanation
We have added a convert_size_to_mb function that takes the file size in bytes and converts it to megabytes (MB). It divides the size by the conversion factor (1024 * 1024) to get the size in megabytes and formats it with up to two decimal places.
Get Size of File in GB (Giga Bytes)
This version of the code specifically returns the file size in gigabytes (GB):
Code
import os
def get_file_size(file_path):
try:
size = os.path.getsize(file_path)
return size
except OSError:
print("File not found or inaccessible.")
return None
def convert_size_to_gb(size_bytes):
# Convert bytes to gigabytes (GB)
size_gb = size_bytes / (1024 * 1024 * 1024)
# Format the size with up to two decimal places
size_gb = "{:.2f}".format(size_gb)
return size_gb
# Example usage
file_path = "path/to/your/file.ext"
file_size = get_file_size(file_path)
if file_size is not None:
size_in_gb = convert_size_to_gb(file_size)
print(f"The size of '{file_path}' is {size_in_gb} GB.")
Output
Depends on file
Explanation
We have added a convert_size_to_gb function that takes the file size in bytes and converts it to gigabytes (GB). It divides the size by the conversion factor (1024 * 1024 * 1024) to get the size in gigabytes and formats it with up to two decimal places.