How to Get File Size in Python | DigitalOcean (2024)

DigitalOcean
    • Log in to:
    • Community
    • DigitalOcean
    • Sign up for:
    • Community
    • DigitalOcean

Tutorial

Published on August 3, 2022

  • Python

How to Get File Size in Python | DigitalOcean (2)

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 pythonimport osfile_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:

How to Get File Size in Python | DigitalOcean (3)

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 pythonimport osfile_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
How to Get File Size in Python | DigitalOcean (4)

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

How to Get File Size in Python | DigitalOcean (5)

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

How to Get File Size in Python | DigitalOcean (6)

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

    Popular Topics

    • Ubuntu
    • Linux Basics
    • JavaScript
    • Python
    • MySQL
    • Docker
    • Kubernetes
    • All tutorials
    • Talk to an expert

    Join the Tech Talk

    Featured on Community

    How to Get File Size in Python | DigitalOcean (8)

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Sign up

    How to Get File Size in Python | DigitalOcean (9)

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Learn more

    How to Get File Size in Python | DigitalOcean (10)

    Become a contributor

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

    Learn more

    Featured Tutorials

    Kubernetes CourseLearn Python 3Machine Learning in PythonGetting started with GoIntro to Kubernetes

    DigitalOcean Products

    App PlatformVirtual MachinesManaged DatabasesManaged KubernetesBlock StorageObject StorageMarketplaceVPCLoad Balancers

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more

    © 2024 DigitalOcean, LLC.Sitemap.

    How to Get File Size in Python | DigitalOcean (2024)
    Top Articles
    Here are the top 5 mutual fund categories for long-term investments | Mint
    Microsoft Office 2021 vs 2019 vs 2016
    What Is Single Sign-on (SSO)? Meaning and How It Works? | Fortinet
    Cold Air Intake - High-flow, Roto-mold Tube - TOYOTA TACOMA V6-4.0
    Gomoviesmalayalam
    Algebra Calculator Mathway
    The Witcher 3 Wild Hunt: Map of important locations M19
    Top tips for getting around Buenos Aires
    Interactive Maps: States where guns are sold online most
    Justified Official Series Trailer
    Hellraiser III [1996] [R] - 5.8.6 | Parents' Guide & Review | Kids-In-Mind.com
    Lowe's Garden Fence Roll
    Metro Pcs.near Me
    Melendez Imports Menu
    Craigslist Northfield Vt
    Construction Management Jumpstart 3Rd Edition Pdf Free Download
    Chime Ssi Payment 2023
    Sienna
    Amelia Chase Bank Murder
    11526 Lake Ave Cleveland Oh 44102
    Grave Digger Wynncraft
    A Man Called Otto Showtimes Near Carolina Mall Cinema
    Core Relief Texas
    Ups Drop Off Newton Ks
    950 Sqft 2 BHK Villa for sale in Devi Redhills Sirinium | Red Hills, Chennai | Property ID - 15334774
    Mawal Gameroom Download
    Craig Woolard Net Worth
    Roadtoutopiasweepstakes.con
    Fandango Pocatello
    Lucky Larry's Latina's
    Giantess Feet Deviantart
    Why Gas Prices Are So High (Published 2022)
    Wisconsin Women's Volleyball Team Leaked Pictures
    Gvod 6014
    Flags Half Staff Today Wisconsin
    The Listings Project New York
    Poe Self Chill
    Unlock The Secrets Of "Skip The Game" Greensboro North Carolina
    Trending mods at Kenshi Nexus
    Swsnj Warehousing Inc
    26 Best & Fun Things to Do in Saginaw (MI)
    Bridgeport Police Blotter Today
    Aurora Southeast Recreation Center And Fieldhouse Reviews
    Rite Aid | Employee Benefits | Login / Register | Benefits Account Manager
    Aznchikz
    Minterns German Shepherds
    Pelican Denville Nj
    Assignation en paiement ou injonction de payer ?
    Game Like Tales Of Androgyny
    Amourdelavie
    Tamilblasters.wu
    Latest Posts
    Article information

    Author: Domingo Moore

    Last Updated:

    Views: 6099

    Rating: 4.2 / 5 (73 voted)

    Reviews: 88% of readers found this page helpful

    Author information

    Name: Domingo Moore

    Birthday: 1997-05-20

    Address: 6485 Kohler Route, Antonioton, VT 77375-0299

    Phone: +3213869077934

    Job: Sales Analyst

    Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

    Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.