SoFunction
Updated on 2025-03-05

.st_size, get file size in python

1. ().st_size

(filePath) Returns the relevant attributes of the specified file to be read and then processed using the stat module.

import os
('data_feather_ys.feather')
# os.stat_result(st_mode=33206, st_ino=3659174697257342, st_dev=2829373452, st_nlink=1, st_uid=0, st_gid=0, st_size=400102338, st_atime=1631499025, st_mtime=1631499025, st_ctime=1631499025)
('data_feather_ys.feather').st_size
# 400102338

two,()

Returns the size of the specified file. When the specified path does not exist or is inaccessible, an exception will be thrown. Implementation form:

def getsize(filename):
	#The editor has created a Python learning exchange group: 725638078    """Return the size of a file, reported by ()."""
    return (filename).st_size

If you want to achieve optimal performance, use () to check whether the path is a file first, and then call st_size.

If you want to use (), you must use () in advance to determine whether it is a file or not before using it.

3. Function encapsulation

Use () to get the file size (unit: MB).

import os
def get_FileSize(filePath):
    filePath = str(filePath)
    fsize = (filePath)
    fsize = fsize / float(1024 * 1024)
    return round(fsize, 2)
print(get_FileSize('data_feather_ys.feather'))

This is the article about obtaining file sizes in python. This is all about this article about obtaining file sizes in python. For more related python ().st_size and () content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!