Use os.path.getsize(path) which will

Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible.

import os
os.path.getsize('C:\\Python27\\Lib\\genericpath.py')

Or use os.stat(path).st_size

import os
os.stat('C:\\Python27\\Lib\\genericpath.py').st_size 

Or use Path(path).stat().st_size (Python 3.4+)

from pathlib import Path
Path('C:\\Python27\\Lib\\genericpath.py').stat().st_size
Answer from Artsiom Rudzenka on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-file-size-in-bytes-kb-mb-and-gb-using-python
Get File Size in Bytes, Kb, Mb, And Gb using Python - GeeksforGeeks
July 23, 2025 - ... import os file_path = 'example.txt' file_info = os.stat(file_path) file_size_bytes = file_info.st_size # Conversion to kilobytes, megabytes, and gigabytes file_size_kb = file_size_bytes / 1024 file_size_mb = file_size_kb / 1024 file_size_gb ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-get-file-size-in-python
How to Get File Size in Python with os and pathlib | DigitalOcean
September 8, 2025 - To display sizes in a human-readable format, you can use a helper function that divides the size by 1024 repeatedly and appends the correct unit: def format_size(size_bytes, decimals=2): if size_bytes == 0: return "0 Bytes" power = 1024 units = ["Bytes", "KB", "MB", "GB", "TB", "PB"] import ...
🌐
PYnative
pynative.com › home › python › file handling › python check file size
Python Get File Size [4 Ways] – PYnative
December 29, 2021 - From Python 3.4 onwards, we can use the pathlib module, which provides a wrapper for most OS functions. Import pathlib module: Pathlib module offers classes and methods to handle filesystem paths and get data related to files for different operating systems. Next, Use the pathlib.Path('path').stat().st_size attribute to get the file size in bytes ... import pathlib # calculate file size in KB, MB, GB def convert_bytes(size): """ Convert bytes to KB, or MB or GB""" for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: if size < 1024.0: return "%3.1f %s" % (size, x) size /= 1024.0 path = pathlib.Path(r'E:/demos/account/sales.txt') f_size = path.stat().st_size print('File size in bytes', f_size) # you can skip this if you don't want file size in KB or MB x = convert_bytes(f_size) print('file size is', x)Code language: Python (python)
🌐
Medium
medium.com › @ryan_forrester_ › getting-file-sizes-in-python-a-complete-guide-01293aaa68ef
Getting File Sizes in Python: A Complete Guide | by ryan | Medium
October 24, 2024 - import os file_path = "example.txt" size_bytes = os.path.getsize(file_path) print(f"File size: {size_bytes} bytes") ... def convert_size(size_bytes): """Convert bytes to human readable format.""" for unit in ['B', 'KB', 'MB', 'GB', 'TB']: if ...
🌐
Turing
turing.com › kb › how-to-get-the-size-of-file-in-python
Different Ways to Get the Size of a File in Python
As shown in the code snippet below, the stat function is an easy way for Python to get the file size in bytes. The relative path to the file is used as an argument for the stat function which is used to obtain the statistics of the file. In the example below, a .csv file was used.
🌐
datagy
datagy.io › home › python posts › how to get file size in python in bytes, kb, mb, and gb
How to Get File Size in Python in Bytes, KB, MB, and GB • datagy
November 4, 2022 - All the methods covered in this tutorial return the size of a file in bytes. However, we can easily get the size of a file in Python in KB, MB, and GB using a custom function.
🌐
AmiraData
amiradata.com › home › learning › python
Python Get File Size in KB, MB or GB - AmiraData
September 10, 2020 - import os from pathlib import Path ... return size def convert_bytes(size, unit=None): if unit == "KB": return print('File size: ' + str(round(size / 1024, 3)) + ' Kilobytes') elif unit == "MB": return print('File size: ' + ...
Find elsewhere
🌐
thisPointer
thispointer.com › home › filehandling › python: get file size in kb, mb or gb – human-readable format
Python: Get file size in KB, MB or GB - human-readable format - thisPointer
January 17, 2023 - *** Get file size in bytes using ... format like in KB, MB or GB *** Get file size in Kilobyte i.e. KB Size of file is : 162996.35546875 KB Get file size in Megabyte i.e. MB Size of file is : 159.17612838745117 MB Get file size ...
🌐
Vultr
docs.vultr.com › python › examples › check-the-file-size
Python Program to Check the File Size
September 27, 2024 - Checking the file size in Python is straightforward using the os module's os.path.getsize() method or the more modern Pathlib approach. Both methods provide efficient ways to retrieve file size, but Pathlib offers enhanced syntax and additional ...
🌐
BTech Geeks
btechgeeks.com › home › get file size python – python: get file size in kb, mb, or gb – human-readable format
Get file size python - Python: Get file size in KB, MB, or GB – human-readable format - BTech Geeks
October 14, 2024 - et file size in human-readable ... == sizeUnit.GB: return sizeInBytes/(1024*1024*1024) else: return sizeInBytes def fileSize(filePath, size_type): """File size in KB, MB and GB"""...
🌐
Devzery
devzery.com › post › how-to-check-file-size-in-python-4-simple-methods
How to Check File Size in Python: 4 Simple Methods - Devzery
September 4, 2024 - While getting the file size in bytes is useful, it's often more practical to display the file size in a more human-readable format, such as ... def convert_bytes(size): for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: if size < 1024.0: return "%3.1f %s" % (size, x) size /= 1024.0 file_size = os.path.getsize('your_file_path') print('File size is', convert_bytes(file_size))
🌐
alpharithms
alpharithms.com › home › tutorials › how to easily get a file size in python
How to Easily Get a File Size in Python - αlphαrithms
June 16, 2022 - The first method relies on the os.path module and the second on the os.stat module. The stat module provides more detailed information but the os.path.getsize() method is more concise. Let’s take a look at how to get file size in Python using both.
🌐
FavTutor
favtutor.com › blogs › get-python-file-size
How to Get File Size in Python? 4 Methods (with code)
September 14, 2023 - Finally, we have printed the total size in bytes of all the files in the directory. In this article, we studied how Python checks file size using four different methods as well as examples and output.
🌐
W3docs
w3docs.com › python
How do I check file size in Python?
import os # Get the size of the file in bytes size = os.path.getsize('file.txt') # Convert the size to kilobytes size_kb = size / 1024 print(f'Size of file: {size_kb:.2f} KB') Copy · This will print · Tags · python file · What does the "yield" ...
🌐
TecAdmin
tecadmin.net › python-program-to-check-the-file-size
Python Program to Check the File Size – TecAdmin
April 26, 2025 - While the os.path.getsize() method provides us with the size of a file in bytes, it may not always be the most user-friendly way to represent file size, especially for larger files. We can create a function to convert the size in bytes to a more readable format such as kilobytes (KB), megabytes ...
🌐
PyTutorial
pytutorial.com › getting-file-size-in-python
PyTutorial | Python: Retrieving File Size in KB, MB, and GB
June 2, 2023 - File size: 1512980 bytes Size in ...size(file_path) # Convert file size to kilobytes and round to 3 decimal places kb = round(file_size / 1024, 3) # Convert kilobytes to megabytes and round to 3 decimal places mb = round(kb / ...