Python format file size

Convert file size to human readable text in Python

This Python tutorial we learn how to use the humanize package to convert file size from a number of bytes into human readable format string.

Install humanize package

Installing the humanize package using the following command.

Convert file size into human readable string

To convert file size in number of bytes into readable string such as K, KiB, MB, GB we can use the naturalsize() method of humanize package as below.

import humanize file_size1 = humanize.naturalsize(3000000) file_size2 = humanize.naturalsize(300, binary=False, gnu=True) file_size3 = humanize.naturalsize(3000, binary=False, gnu=True) file_size4 = humanize.naturalsize(3000, binary=False, gnu=True, format='%.3f') file_size5 = humanize.naturalsize(3000, binary=True) print(file_size1) print(file_size2) print(file_size3) print(file_size4) print(file_size5)
3.0 MB 300B 2.9K 2.930K 2.9 KiB

Источник

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.

Читайте также:  HTML Sample Page Title

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.

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

Источник

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