Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | os.path.isabs() method
Next article icon

Python | os.path.size() method

Last Updated : 15 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependentthe functionality. os.path module is a submodule of the OS module in Python used for common path name manipulation. In this article, we will discuss how to determine the file size with os.path.getsize() in Python.

os.path.getsize() method in Python is used to check the size of a specified path. It returns the size of the raisesspecified path in bytes. The method raises OSError if the file does not exist or is somehow inaccessible.

Python os.path.getsize() method Syntax

Syntax: os. path.getsize(path)

Parameter:

  • path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.

Return Type: This method returns a integer value which represents the size of specified path in bytes.

os.path.getsize() method in Python

Below are the following ways by which we can see how to determine the file size with os.path.getsize() in Python.

Working With os.path.getsize() Method in Python

In this example, the os.path.getsize() method is used to retrieve and display the size of a file located at the specified path (/home/User/Desktop/file.txt) in bytes.

Python3




import os
 
# Path
path = '/home/User/Desktop/file.txt'
 
size = os.path.getsize(path)
 
print("Size (In bytes) of '%s':" % path, size)
 
 
Output
Size (In bytes) of '/home/User/Desktop/file.txt': 243  

Handling Error While Using os.path.getsize() Method in Python

In this example, the script attempts to retrieve the size of a file located at the specified path (/home/User/Desktop/file2.txt). If the file does not exist or is inaccessible, it prints an error message and exits the program.

Python3




import os
 
# Path
path = '/home/User/Desktop/file2.txt'
 
try:
    size = os.path.getsize(path)
 
except OSError:
    print("Path '%s' does not exists or is inaccessible" % path)
    sys.exit()
 
print("Size (In bytes) of '% s':" % path, size)
 
 
Output
Path '/home/User/Desktop/file2.txt' does not exists or is inaccessible  

Finding the Sizes of Multiple Files

In this example, the script iterates over a list of file paths (file1.txt, file2.txt, file3.txt) and retrieves the size of each file in bytes using the os.path.getsize() method, then prints the sizes alongside their respective filenames.

Python3




import os
 
# List of file paths
file_paths = ['file1.txt', 'file2.txt', 'file3.txt']
 
# Get and print the sizes of each file
for file_path in file_paths:
    size = os.path.getsize(file_path)
    print(f"Size of '{file_path}' in bytes:", size)
 
 

Output:

Size of 'file1.txt' in bytes: 1234
Size of 'file2.txt' in bytes: 5678
Size of 'file3.txt' in bytes: 9012



Next Article
Python | os.path.isabs() method

I

ihritik
Improve
Article Tags :
  • Python
  • python-os-module
Practice Tags :
  • python

Similar Reads

  • OS Path module in Python
    OS Path module contains some useful functions on pathnames. The path parameters are either strings or bytes. These functions here are used for different purposes such as for merging, normalizing, and retrieving path names in Python. All of these functions accept either only bytes or only string obje
    2 min read
  • os.path.abspath() method - Python
    The os.path.abspath() method in Python's os module is used to get the full (absolute) path of a file or folder. It's useful when you're working with relative paths but need to know the exact location on your system. If you pass a relative path, it converts it to an absolute one and if the path is al
    2 min read
  • Python | os.path.basename() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man
    4 min read
  • Python | os.path.commonpath() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common path name mani
    2 min read
  • Python | os.path.commonprefix() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common path name mani
    2 min read
  • Python | os.path.dirname() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man
    2 min read
  • Python | os.path.exists() method
    os.path.exists() method in Python is used to check whether the specified path exists or not. This method can be also used to check whether the given path refers to an open file descriptor or not. os.path.exists() Syntax in PythonSyntax: os.path.exists(path) Parameter: path: A path-like object repres
    2 min read
  • Python | os.path.lexists() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in python used for common path name man
    2 min read
  • Python | os.path.expanduser() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common pathname mani
    4 min read
  • Python | os.path.expandvars() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path the module is a submodule of OS module in Python used for common pathname
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences