How to Get the File Size using Python

To get the file size (in bytes) using Python:

import os

file_path = r"path where the file is stored\file_name.file_extension"
file_size = os.path.getsize(file_path)

print(file_size)

Optionally, to get the size in bytes, kilobytes, megabytes and gigabytes:

import os

file_path = r"path where the file is stored\file_name.file_extension"
file_size = os.path.getsize(file_path)

print("File size in Bytes: " + str(file_size))
print("File size in Kilobytes: " + str(file_size/1024))
print("File size in Megabytes: " + str(file_size/1024**2))
print("File size in Gigabytes: " + str(file_size/1024**3))

Steps to Get the File Size Using Python

Step 1: Capture the path where the file is stored

To begin, capture the path where your file is stored.

For demonstration purposes, let’s suppose that the following Text file (called ‘products‘) is currently stored under the following path:

C:\Users\Ron\Desktop\Test\products.txt

Where the file size is 1.95 MB:

products    1.95 MB

The ultimate goal is to get that file size using Python.

Step 2: Get the file size using Python

To get the file size in bytes:

import os

file_path = r"C:\Users\Ron\Desktop\Test\products.txt"
file_size = os.path.getsize(file_path)

print(file_size)

The file size is 2047652 in bytes:

2047652

Step 3 (Optional): Get the file size in bytes, kilobytes, megabytes and gigabytes

Optionally, you may use the syntax below to get the size in bytes, kilobytes, megabytes and gigabytes:

import os

file_path = r"C:\Users\Ron\Desktop\Test\products.txt"
file_size = os.path.getsize(file_path)

print("File size in Bytes: " + str(file_size))
print("File size in Kilobytes: " + str(file_size/1024))
print("File size in Megabytes: " + str(file_size/1024**2))
print("File size in Gigabytes: " + str(file_size/1024**3))

The result:

File size in Bytes: 2047652
File size in Kilobytes: 1999.66015625
File size in Megabytes: 1.9527931213378906
File size in Gigabytes: 0.0019070245325565338

Get the File Size using Pathlib

You can also get the file size in bytes using the pathlib library (for Python 3.4 and above):

from pathlib import Path

file_path = r"C:\Users\Ron\Desktop\Test\products.txt"
file_size = Path(file_path).stat().st_size

print(file_size)

You’ll then see the result in bytes:

2047652

Alternatively, the code below can be used to get the size in bytes, kilobytes, megabytes and gigabytes:

from pathlib import Path

file_path = r"C:\Users\Ron\Desktop\Test\products.txt"
file_size = Path(file_path).stat().st_size

print("File size in Bytes: " + str(file_size))
print("File size in Kilobytes: " + str(file_size/1024))
print("File size in Megabytes: " + str(file_size/1024**2))
print("File size in Gigabytes: " + str(file_size/1024**3))

The result:

File size in Bytes: 2047652
File size in Kilobytes: 1999.66015625
File size in Megabytes: 1.9527931213378906
File size in Gigabytes: 0.0019070245325565338

Additional Resources

You may check the os.path documentation to learn more about os.path.getsize.

Alternatively, you may visit the pathlib documentation.