Tutorial

How to Get File Size in Python

Published on August 3, 2022
author

Pankaj

How to Get File Size in Python

We can get file size in Python using the os module.

File Size in Python

The python os module has stat() function where we can pass the file name as argument. This function returns a tuple structure that contains the file information. We can then get its st_size property to get the file size in bytes. Here is a simple program to print the file size in bytes and megabytes.

# get file size in python
import os

file_name = "/Users/pankaj/abcdef.txt"

file_stats = os.stat(file_name)

print(file_stats)
print(f'File Size in Bytes is {file_stats.st_size}')
print(f'File Size in MegaBytes is {file_stats.st_size / (1024 * 1024)}')

Output:

File Size In Python
File Size In Python

If you look at the stat() function, we can pass two more arguments: dir_fd and follow_symlinks. However, they are not implemented for Mac OS. Here is an updated program where I am trying to use the relative path but it’s throwing NotImplementedError.

# get file size in python
import os

file_name = "abcdef.txt"
relative_path = os.open("/Users/pankaj", os.O_RDONLY)

file_stats = os.stat(file_name, dir_fd=relative_path)

Output:

Traceback (most recent call last):
  File "/Users/pankaj/.../get_file_size.py", line 7, in 
    file_stats = os.stat(file_name, dir_fd=relative_path)
NotImplementedError: dir_fd unavailable on this platform
Python File Size Relative Path NotImplementedError
Python File Size Relative Path NotImplementedError

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 7, 2020

Thanks for the example. It was helpful. As an FYI I use a mac os version 10.15.5. The get info feature of Finder reports file size in multiples of 1,000,000 ( 1000 * 1000) not 1,048,596 (1024 * 1024). Not sure if this changed at some point. Thanks again

- John

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.