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

How to Get the File Size using Python

As you can see from the image above, the file size is 2,187 KB (or 2239488 in bytes).

The ultimate goal is to get this 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:

Number in bytes

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:

Get the File Size using Python

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:

Bytes

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:

How to Get the File Size using Python

Additional Resources

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

Alternatively, you may visit the pathlib documentation.