How to Get the File Size using Python

You can use the syntax below in order to get the file size (in bytes) using Python:

import os.path

file_path = r'path where the file is stored\file name.file extension'
file_size = os.path.getsize(file_path)

print(file_size)

Optionally, you can use the following code to get the size in bytes, kilobytes, megabytes and gigabytes:

import os.path

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))

Later, you’ll also see how to obtain the file size using the pathlib library.

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

Where the file size is 2,187 KB (or 2239488 in bytes):

Products    2,187 KB

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

Step 2: Get the file size using Python

You can use this template to get the file size in bytes:

import os.path

file_path = r'path where the file is stored\file name.file extension'
file_size = os.path.getsize(file_path)

print(file_size)

For our example:

  • The path where the file is stored is: C:\Users\Ron\Desktop\Test
  • The file name is: Products
  • The file extension is: txt

Therefore, the complete code to get the file size in bytes is:

import os.path

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

print(file_size)

As you can see, the file size is indeed 2239488 in bytes:

2239488

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.path

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))

Here is the complete code in the context of our example:

import os.path

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))

You’ll now see the file size as follows:

File size in Bytes: 2239488
File size in Kilobytes: 2187.0
File size in Megabytes: 2.1357421875
File size in Gigabytes: 0.0020856857299804688

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:

2239488

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))

Here is the result:

File size in Bytes: 2239488
File size in Kilobytes: 2187.0
File size in Megabytes: 2.1357421875
File size in Gigabytes: 0.0020856857299804688

Additional Resources

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

Alternatively, you may visit the pathlib documentation.