1/17/2023 0 Comments Size of a check![]() The complete example code is as follows: import osįile_size = os.path.getsize(r'C:\test\file1.txt')Ĭheck File Size in Python Using the os.stat Method This Python’s module os.path has a function getsize that returns the file size in bytes by taking the file path as the argument. ![]() We must place r before making the file path as the raw string otherwise, we need to escape the backslash, like C:\\test\\file1.txt.Ĭheck File Size in Python Using the os.path() Method Print("The file size is:", file_size,"bytes") The complete example code is as follows: from pathlib import Pathįile_size =Path(r'C:\test\file1.txt').stat().st_size st_size attribute of the stat method gives the file size in bytes. ![]() The stat() method of the Path object returns the detailed properties of the file like st_mode, st_dev etc. Check File Size in Python Using the pathlib Module This article will introduce different methods to check the file size in Python. It can be measured in different memory units like kilo bytes(kB), mega bytes(MB), and gigabytes(GB) etc. The file size evaluates how much space a file will take to store in a storage medium like a hard disk.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |