All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Get File Size

Last Updated : Mar 11, 2024

Python Get File Size

In this article we will show you the solution of python get file size, Python offers a number of approaches to decide a file's length. The document length is a measure of ways an awful lot disc space a report takes up.

The os.Course module, which allows you to retrieve file-related data, is one standard approach.

You can get the report's size in bytes by means of calling the os.path.getsize() characteristic and imparting the document's route as an argument.

Another approach is getting a stat object with comprehensive information about the file by using the os.stat() function.

You can obtain the file size by gaining access to the stat object's st_size field.

Additionally, utilising the Path.stat().st_size feature, the Path class from the pathlib module offers a platform-independent approach to manage file paths and retrieve file sizes.

These techniques make it possible to retrieve file sizes programmatically, allowing you to effectively perform size-based actions or examine storage utilisation in Python.

Now move to the concept of python get file size.

Step By Step Guide On Python Get File Size :-

import os
from pathlib import Path
def get_file_size_1(file_path):
    size = os.path.getsize(file_path)
    return size
def get_file_size_2(file_path):
    file_stats = os.stat(file_path)
    size = file_stats.st_size
    return size
def get_file_size_3(file_path):
    path_obj = Path(file_path)
    size = path_obj.stat().st_size
    return size
file_path = "path/to/your/file.txt"
size_1 = get_file_size_1(file_path)
print("Method 1 - File Size:", size_1, "bytes")
size_2 = get_file_size_2(file_path)
print("Method 2 - File Size:", size_2, "bytes")
size_3 = get_file_size_3(file_path)
print("Method 3 - File Size:", size_3, "bytes")
  1. To work with file-related operations, we import the required modules, os and pathlib.
  2. Then, three functions are defined, each of which has a unique approach for determining file sizes.
  3. The os.path.getsize() function is used in the first method, get_file_size_1.
  4. The file size in bytes is returned by this function after receiving the file_path as an argument.
  5. We return this size after storing it in the size variable.
  6. The second technique, get_file_size_2, makes use of os.stat().
  7. It assigns the file_stats variable with the file statistics it has obtained using os.stat(file_path).
  8. We can retrieve the st_size attribute, which denotes the file size in bytes, from the file_stats object.
  9. We return this size after storing it in the size variable.
  10. The pathlib module's Path class is used by the third function, get_file_size_3.
  11. By providing the file_path to Path(), we produce a Path object, path_obj.
  12. Then, we access the file statistics & extract the file size in bytes using path_obj.stat().st_size.
  13. This size is returned after being assigned to the size variable.
  14. We test the functions after they had been described by means of passing a file path to the variable file_path.
  15. Replace "path/to/your/file.txt" with the suitable path to the file whose length you need to recognize.
  16. The three functions are then referred to as, every with the argument file_path, and the ensuing sizes are saved within the variables size_1, size_2, and size_3.
  17. Finally, we use print() commands to output the sizes together with an informative label.

Conclusion :-

As a result, we were able to understand the obtain file size in Python notion.

Additionally, we discovered that there are numerous ways to use the os.path.getsize(), os.stat(), and Path.stat().st_size attributes from the pathlib module to determine the size of a file in Python.

We can use these techniques to programmatically retrieve the file size in bytes.

I hope this article on python get file size helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪