Skip to content

How to Get File Size in Python in Bytes, KB, MB, and GB

How to Get File Size in Python in Bytes, KB, MB, and GB Cover Image

When working with files programmatically, you’ll often want to know how large a file is. This can be helpful when transferring files or copying files. Python provides a number of built-in methods of doing this, including using the os and pathlib libraries. Depending on your preference, either of these approaches works well.

By the end of this tutorial, you’ll have learned:

  • How to get the file size in Python using os and pathlib
  • How to get the file size in Python in bytes, KB, MB, and GB

How to Use Python os to Get a File Size

The Python os library provides two different ways to get the size of a file: the os.path.getsize() function and the os.stat() function. The getsize() function, as the name implies, returns the size of the file. On the other hand, the stat() function returns a number of statistics, or attributes, about the file.

Let’s see how both of these functions can be used to use Python to get a file size.

How to Use os.path.getsize() to Get File Size

In this section, you’ll learn how to use the getsize function to get the size of a file. The function accepts a file path pointing to the file and returns the size in bytes. Let’s see how we can use the function to get the file size:

# Get the Size of a File Using getsize()
import os

file_path = '/Users/datagy/Desktop/file.py'
print(os.path.getsize(file_path))

# Returns: 2453

In the example above, we use the getsize function and pass in a string containing the path to a file. If the file doesn’t exist, Python will raise a PermissionError, indicating that no file could be found.

One of the benefits of the function is how explicit it is. It lets the user know immediately that you’re hoping to get the size of a file.

How to Use os.stat() to Get File Size

The Python os stat function returns a stat_result object which provides information about the file itself. Included in this are items such as the size and owner information.

# Get Size of a File Using os.stat()
import os

file_path = '/Users/datagy/Desktop/file.py'
print(os.stat(file_path))

# Returns: os.stat_result(st_mode=33188, st_ino=69380148, st_dev=16777233, st_nlink=1, st_uid=501, st_gid=20, st_size=2453, st_atime=1665780677, st_mtime=1662640208, st_ctime=1665780674)

We can then access the size of the file by accessing the .st_size attribute. Let’s see how we can do this with Python:

# Get Size of a File Using os.stat()
import os

file_path = '/Users/datagy/Desktop/file.py'
print(os.stat(file_path.st_size))

# Returns: st_size=2453

In the code block above, we can see that by accessing the .st_size attribute, the size is returned in bytes. The function returns an item that’s like a NamedTuple – where we can access items based on their named attributes. This is shown below:

# Checking the Type of an os.stat object
import os

file_path = '/Users/datagy/Desktop/file.py'
print(type(os.stat(file_path)))

# Returns: <class 'os.stat_result'>

In the code block above, we access the type of the returned value, which is like a NamedTuple.

How to Use Python Pathlib to Get a File Size

The Python Pathlib library also allows you to access information about files, including the size of a file. The Pathlib library provides an object-oriented interface for working with files. In order to access the size of a file, we can use the .stat() method and access the .st_size attribute. Let’s see what this looks like:

# Using Pathlib to Get the Size of a File in Python
import pathlib

file_path = '/Users/datagy/Desktop/file.py'
path = pathlib.Path(file_path)

print(path.stat().st_size)

# Returns: 2453

In order to get the size of a file in Python using the pathlib library, you can:

  1. Import the pathlib library

    Because the library is built into Python, you don’t need to install it first

  2. Create a path object by instantiating a Path() object

    The pathlib Path object can be created using pathlib.Path()

  3. Access the size using the .stat().st_size attribute

    By applying the .stat() method and accessing the .st_size attribute, you can access the size of a file in bytes.

How to Use Python to Get the File Size in KB, MB, or GB

All the methods covered in this tutorial return the size of a file in bytes. However, we can easily get the size of a file in Python in KB, MB, and GB using a custom function. In order to do this, let’s write a function that allows you to pass in a file path and the format you want to return the size in.

# Getting the Size of a File in Python Using Different Units
import os
file_path = '/Users/datagy/Desktop/LargeFile.MOV'

def get_size(file_path, unit='bytes'):
    file_size = os.path.getsize(file_path)
    exponents_map = {'bytes': 0, 'kb': 1, 'mb': 2, 'gb': 3}
    if unit not in exponents_map:
        raise ValueError("Must select from \
        ['bytes', 'kb', 'mb', 'gb']")
    else:
        size = file_size / 1024 ** exponents_map[unit]
        return round(size, 3)


print(get_size(file_path, 'mb'))

# Returns: 104.062

Let’s break down what the code block above is doing:

  1. We import the os library and instantiate our file_path variable
  2. We then create a function get_size() which takes two parameters
  3. The function first gets the size in bytes
  4. It then creates a mapping object that maps units to an exponent
  5. The function then first checks if the unit is available to be calculated. If not, it raises a ValueError.
  6. Finally, the function returns the file size divided by 1024 raised to the power of the unit

Conclusion

In this tutorial, you learned how to use Python to get the size of a file. You first learned how to use the os library to explore two different methods of getting the size of a file. Then, you learned how to use the Python pathlib library to get the size of a file. Finally, you learned how to use these methods to convert the sizes of files to KB, MB, and GB.

Additional Resources

To learn more about related topics, check out the tutorials below:

Leave a Reply

Your email address will not be published.