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

linux - How to get the size of tar.gz in (MB) file in python - Stack Overflow
I am doing backups in python script but i need to get the size of tar.gz file created in MB How can i get the size in MB of that file More on stackoverflow.com
🌐 stackoverflow.com
How do I check file size in Python? - Stack Overflow
from pathlib import Path file = ... file.stat().st_size · This is really only an interface around os.stat, but using pathlib provides an easy way to access other file related operations. ... There is a bitshift trick I use if I want to to convert from bytes to any other unit. If you do a right shift by 10 you basically shift it by an order (multiple). ... print (5368709120 >> 10) # 5242880 kilobytes (kB) print (5368709120 ... More on stackoverflow.com
🌐 stackoverflow.com
Is there function to know raster's size (KB) with python? - Geographic Information Systems Stack Exchange
There I have many raster in canvas ... to know size in MB. I am searching function in python that give me this result. ... Try referring to this Stack Overflow post. They are looking at zip files, but it should work fine for your raster file. If you are using a type of raster that is made up of multiple files, then I would suggest getting the file size ... More on gis.stackexchange.com
🌐 gis.stackexchange.com
October 8, 2014
Need to resize multiple images to a specific size in KB. How do I achieve this?
Assuming these are JPEGs, the amount of compression achieved at any particular quality level is highly dependent on the detail of the individual image. So I don't think it's possible to predict the size of the compressed image without actually doing the compression. So I think your best bet is to iterate through different quality levels for each image until you find one that gives you the output size you want. More on reddit.com
🌐 r/learnpython
15
3
May 22, 2023
🌐
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 ...
🌐
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.
🌐
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 ...
Find elsewhere
🌐
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: ' + ...
🌐
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.
🌐
Vultr
docs.vultr.com › python › examples › check-the-file-size
Python Program to Check the File Size | Vultr Docs
September 27, 2024 - Create a Path object and use its stat() method to get file details. ... from pathlib import Path file_path = Path('example.txt') file_size = file_path.stat().st_size print("File Size in Bytes:", file_size) Explain Code · This block of code ...
🌐
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.
🌐
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 ...
🌐
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.
🌐
Enterprise DNA
blog.enterprisedna.co › how-to-get-file-size-in-python-a-quick-guide
How to Get File Size in Python: A Quick Guide – Master Data Skills + AI
There are two common techniques ... in Python: the seek() and tell() methods. Both methods manipulate the file pointer or cursor. The file pointer represents the current position in the file from where the next read or write operation can take place. To find the size of a file ...
🌐
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 / ...