Get File Size in Bytes, Kb, Mb, And Gb using Python
Last Updated :
09 Feb, 2024
Handling file sizes in Python is a common task, especially when working with data processing, file management, or simply understanding resource usage. Fortunately, Python provides several methods to obtain file sizes in various units, such as bytes, kilobytes, megabytes, and gigabytes. In this article, we will explore five simple and generally used methods to achieve this.
Get File Size In Python In Bytes, Kb, Mb, And Gb
Below, are the methods to Get File Size In Python In Bytes, Kb, Mb, And Gb in Python.
Example 1: Using os.path.getsize()
The os.path.getsize()
function from the os
module is a straightforward way to obtain the size of a file in bytes.
This method directly returns the size of the file in bytes.
In this example, the code uses Python OS Module to get the size of a file named 'example.txt'. It then converts the file size from bytes to kilobytes, megabytes, and gigabytes and prints the results with two decimal places.
Python3
import os
file_path = 'example.txt'
file_size_bytes = os.path.getsize(file_path)
# Conversion to kilobytes, megabytes, and gigabytes
file_size_kb = file_size_bytes / 1024
file_size_mb = file_size_kb / 1024
file_size_gb = file_size_mb / 1024
print(f"File Size (Bytes): {file_size_bytes} B")
print(f"File Size (KB): {file_size_kb:.2f} KB")
print(f"File Size (MB): {file_size_mb:.2f} MB")
print(f"File Size (GB): {file_size_gb:.2f} GB")
Output :
File Size (Bytes): 2359 B
File Size (KB): 2.30 KB
File Size (MB): 0.00 MB
File Size (GB): 0.00 GB
Example 2: Using os.stat()
Method
The os.stat()
function can provide more detailed information about a file, including its size. This method allows for greater flexibility in extracting various file attributes.
In this example, code utilizes Python's `os` module to gather information about the file 'example.txt', including its size in bytes. It then performs conversions to represent the file size in kilobytes, megabytes, and gigabytes. The final output displays the file size in each unit with two decimal places.
Python3
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 = file_size_mb / 1024
print(f"File Size (Bytes): {file_size_bytes} B")
print(f"File Size (KB): {file_size_kb:.2f} KB")
print(f"File Size (MB): {file_size_mb:.2f} MB")
print(f"File Size (GB): {file_size_gb:.2f} GB")
Output :
File Size (Bytes): 2359 B
File Size (KB): 2.30 KB
File Size (MB): 0.00 MB
File Size (GB): 0.00 GB
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read