All TalkersCode Topics

Follow TalkersCode On Social Media

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

Python Get File Size In MB

Last Updated : Mar 11, 2024

Python Get File Size In MB

In this article we will show you the solution of python get file size in mb, there are times when you want to discover the sizes of a file in megabytes (MB) the usage of Python's file management machine.

This facts can be used to determine the size of files being processed or the quantity of disk area being used, among other matters.

To get this data, you could use the getsize() feature from the os.path module in Python.

Just skip the record course to this characteristic and you'll get the file size in bytes.

For added evaluation on your Python applications, it is feasible to determine the exact length in megabytes by multiplying the byte size by using either 1024 or 1,048,576.

Use this calculation to help in decision-making procedures. Now move to the concept of python get file size in mb.

Step By Step Guide On Python Get File Size In MB :-

import os
def get_file_size(file_path):
    if os.path.isfile(file_path):
        size_in_bytes = os.path.getsize(file_path)
        size_in_mb = size_in_bytes / (1024 * 1024)
        return size_in_mb
    else:
        print("File not found.")
        return None
file_path = '/path/to/your/file.txt'
file_size_mb = get_file_size(file_path)
if file_size_mb is not None:
    print(f"The file size is {file_size_mb} MB.")
  1. The os module, which enables communication with the operating system, is imported first.
  2. This module offers functions for numerous operations involving files.
  3. Next, we create the get_file_size feature, which accepts a file_path as an issue.
  4. This function's goal is to go back the file's megabyte size.
  5. To determine whether or not the furnished file route results in a legitimate file, we utilise the os.Course.Isfile(file_path) situation inside the feature.
  6. If it does, we circulate on to identifying the file size.
  7. Using the os.Direction.Getsize(file_path) feature, which returns the document's length in bytes, we will decide the record length.
  8. This price is saved within the size_in_bytes variable.
  9. We divide size_in_bytes by way of (1024 * 1024) to get the document length in megabytes.
  10. This is due to the reality that a kilobyte has 1024 bytes and a megabyte has 1024 kilobytes. The variable size_in_mb holds the final results.
  11. We go back the size_in_mb value if the document is actual and the size calculation labored.
  12. The message "File not found." is printed and we return None if the document would not exist on the file_path that was provided.
  13. The file_path variable is about to the course of the record we desire to research inside the pattern usage segment.
  14. The get_file_size function is then invoked with file_path as the enter. The fee received is saved in file_size_mb.
  15. Finally, we make sure that file_size_mb isn't always None. If it has a value, an organized string is used to print the record length in megabytes.

Conclusion: :-

As a result, we were capable of understand the idea in the back of Python's get file length in megabytes.

We also located that using the os.Path module as well as the getsize() function, we are able to use Python to determine the record length in megabytes.

We can convert the size in bytes to megabytes with the aid of dividing it by means of 1,048,576.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪