Python get file sizes

Python: Get file size in KB, MB or GB – human-readable format

In this article, we will discuss different ways to get file size in human-readable formats like Bytes, Kilobytes (KB), MegaBytes (MB), GigaBytes(GB) etc.

Different ways to get file size in Bytes

Get file size in bytes using os.path.getsize()

It accepts the file path as an argument and returns the size of a file at the given path in bytes.
If the file doesn’t exist at the given path or it is inaccessible, then it raises an os.error. Therefore, always check that file exist or not before calling this function.

Let’s use this function to get the size of a file in bytes,

import os def get_file_size_in_bytes(file_path): """ Get size of file at given path in bytes""" size = os.path.getsize(file_path) return size file_path = 'big_file.csv' size = get_file_size_in_bytes(file_path) print('File size in bytes : ', size)

Frequently Asked:

File size in bytes : 166908268

Get file size in bytes using os.stat().st_size

Python’s os module provides a function to get the file statistics,

os.stat(path, *, dir_fd=None, follow_symlinks=True)

It accepts file path (a string) as an argument and returns an object of the structure stat, which contains various attributes about the file at a given path. One of the attributes is st_size, which has the size of the file in bytes.

Читайте также:  Join two arrays python

Let’s use this function to get the size of a file in bytes,

import os def get_file_size_in_bytes_2(file_path): """ Get size of file at given path in bytes""" # get statistics of the file stat_info = os.stat(file_path) # get size of file in bytes size = stat_info.st_size return size file_path = 'big_file.csv' size = get_file_size_in_bytes_2(file_path) print('File size in bytes : ', size)
File size in bytes : 166908268

Get file size in bytes using pathlib.Path.stat().st_size

Let’s use pathlib module to get the size of a file in bytes,

from pathlib import Path def get_file_size_in_bytes_3(file_path): """ Get size of file at given path in bytes""" # get file object file_obj = Path(file_path) # Get file size from stat object of file size = file_obj.stat().st_size return size file_path = 'big_file.csv' size = get_file_size_in_bytes_3(file_path) print('File size in bytes : ', size)
File size in bytes : 166908268

In all the above techniques, we got the file size in bytes. What if we want file size in human-readable format like, KilloBytes, Megabytes or GigaBytes etc.

Get file size in human-readable units like kilobytes (KB), Megabytes (MB) or GigaBytes (GB)

1 KilloByte == 1024 Bytes
1 Megabyte == 1024*1024 Bytes
1 GigaByte == 1024*1024*1024 Bytes

We have created a function to convert the bytes into kilobytes (KB), Megabytes (MB) or GigaBytes (GB) i.e.

import enum # Enum for size units class SIZE_UNIT(enum.Enum): BYTES = 1 KB = 2 MB = 3 GB = 4 def convert_unit(size_in_bytes, unit): """ Convert the size from bytes to other units like KB, MB or GB""" if unit == SIZE_UNIT.KB: return size_in_bytes/1024 elif unit == SIZE_UNIT.MB: return size_in_bytes/(1024*1024) elif unit == SIZE_UNIT.GB: return size_in_bytes/(1024*1024*1024) else: return size_in_bytes

Let’s create a function to get the file size in different size units. This function internally uses to the above function to convert bytes into given size unit,

import os def get_file_size(file_name, size_type = SIZE_UNIT.BYTES ): """ Get file in size in given unit like KB, MB or GB""" size = os.path.getsize(file_name) return convert_unit(size, size_type)

Let’s use this function to get the size of a given file in KB, MB or GB,

Get size of a file in Kilobyte i.e. KB

file_path = 'big_file.csv' # get file size in KB size = get_file_size(file_path, SIZE_UNIT.KB) print('Size of file is : ', size , 'KB')
Size of file is : 162996.35546875 KB

Get size of a file in Megabyte i.e. MB

file_path = 'big_file.csv' # get file size in MB size = get_file_size(file_path, SIZE_UNIT.MB) print('Size of file is : ', size , 'MB')
Size of file is : 159.17612838745117 MB

Get size of a file in Gigabyte i.e. GB

file_path = 'big_file.csv' # get file size in GB size = get_file_size(file_path, SIZE_UNIT.GB) print('Size of file is : ', size , 'GB')
Size of file is : 0.15544543787837029 GB

Check if file exists before checking for the size of the file

If the file does not exist at the given path, then all the above created function to get file size can raise Error. Therefore we should first check if file exists or not, if yes then only check its size,

import os file_name = 'dummy_file.txt' if os.path.exists(file_name): size = get_file_size(file_name) print('Size of file in Bytes : ', size) else: print('File does not exist')

As file ‘dummy_file.txt’ does not exist, so we can not calculate its size.

The complete example is as follows,

import os import enum from pathlib import Path def get_file_size_in_bytes(file_path): """ Get size of file at given path in bytes""" size = os.path.getsize(file_path) return size def get_file_size_in_bytes_2(file_path): """ Get size of file at given path in bytes""" # get statistics of the file stat_info = os.stat(file_path) # get size of file in bytes size = stat_info.st_size return size def get_file_size_in_bytes_3(file_path): """ Get size of file at given path in bytes""" # get file object file_obj = Path(file_path) # Get file size from stat object of file size = file_obj.stat().st_size return size # Enum for size units class SIZE_UNIT(enum.Enum): BYTES = 1 KB = 2 MB = 3 GB = 4 def convert_unit(size_in_bytes, unit): """ Convert the size from bytes to other units like KB, MB or GB""" if unit == SIZE_UNIT.KB: return size_in_bytes/1024 elif unit == SIZE_UNIT.MB: return size_in_bytes/(1024*1024) elif unit == SIZE_UNIT.GB: return size_in_bytes/(1024*1024*1024) else: return size_in_bytes def get_file_size(file_name, size_type = SIZE_UNIT.BYTES ): """ Get file in size in given unit like KB, MB or GB""" size = os.path.getsize(file_name) return convert_unit(size, size_type) def main(): print('*** Get file size in bytes using os.path.getsize() ***') file_path = 'big_file.csv' size = get_file_size_in_bytes(file_path) print('File size in bytes : ', size) print('*** Get file size in bytes using os.stat().st_size ***') file_path = 'big_file.csv' size = get_file_size_in_bytes_2(file_path) print('File size in bytes : ', size) print('*** Get file size in bytes using pathlib.Path.stat().st_size ***') file_path = 'big_file.csv' size = get_file_size_in_bytes_3(file_path) print('File size in bytes : ', size) print('*** Get file size in human readable format like in KB, MB or GB ***') print('Get file size in Kilobyte i.e. KB') file_path = 'big_file.csv' # get file size in KB size = get_file_size(file_path, SIZE_UNIT.KB) print('Size of file is : ', size , 'KB') print('Get file size in Megabyte i.e. MB') file_path = 'big_file.csv' # get file size in MB size = get_file_size(file_path, SIZE_UNIT.MB) print('Size of file is : ', size , 'MB') print('Get file size in Gigabyte i.e. GB') file_path = 'big_file.csv' # get file size in GB size = get_file_size(file_path, SIZE_UNIT.GB) print('Size of file is : ', size , 'GB') print('*** Check if file exists before checking for the size of a file ***') file_name = 'dummy_file.txt' if os.path.exists(file_name): size = get_file_size(file_name) print('Size of file in Bytes : ', size) else: print('File does not exist') if __name__ == '__main__': main()
*** Get file size in bytes using os.path.getsize() *** File size in bytes : 166908268 *** Get file size in bytes using os.stat().st_size *** File size in bytes : 166908268 *** Get file size in bytes using pathlib.Path.stat().st_size *** File size in bytes : 166908268 *** Get file size in human readable format like in KB, MB or GB *** Get file size in Kilobyte i.e. KB Size of file is : 162996.35546875 KB Get file size in Megabyte i.e. MB Size of file is : 159.17612838745117 MB Get file size in Gigabyte i.e. GB Size of file is : 0.15544543787837029 GB *** Check if file exists before checking for the size of a file *** File does not exist

Источник

How to Get File Size in Python

How to Get File Size in Python

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

File Size in Python

The python os module has stat() function where we can pass the file name as argument. This function returns a tuple structure that contains the file information. We can then get its st_size property to get the file size in bytes. Here is a simple program to print the file size in bytes and megabytes.

# get file size in python import os file_name = "/Users/pankaj/abcdef.txt" file_stats = os.stat(file_name) print(file_stats) print(f'File Size in Bytes is ') print(f'File Size in MegaBytes is ') 

File Size In Python

Output: If you look at the stat() function, we can pass two more arguments: dir_fd and follow_symlinks. However, they are not implemented for Mac OS. Here is an updated program where I am trying to use the relative path but it’s throwing NotImplementedError.

# get file size in python import os file_name = "abcdef.txt" relative_path = os.open("/Users/pankaj", os.O_RDONLY) file_stats = os.stat(file_name, dir_fd=relative_path) 
Traceback (most recent call last): File "/Users/pankaj/. /get_file_size.py", line 7, in file_stats = os.stat(file_name, dir_fd=relative_path) NotImplementedError: dir_fd unavailable on this platform 

Python File Size Relative Path NotImplementedError

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us

Источник

Оцените статью