How to Get Python File Size From a System?
Python provides the following methods for getting file sizes from the system, as follows:
Method 1: Get the File Size Using “os.path.getsize()” Method
In Python, the “os.path.getsize()” function retrieves the file size in bytes based on the file path. Here’s an example:
Example 1: Get the File Size in “Bytes”
The following code is utilized to get the size of the specified file:
file_path = r'C:\Users\p\Documents\program\filename.txt'
file_size = os.path.getsize(file_path)
print(f"The size of '{file_path}' is {file_size} bytes.")
In the above code:
- The “os” module is imported and the specified file path is defined as a string, respectively.
- The “os.path.getsize()” function is applied to return the file size in bytes.
- Lastly the “f-string” is used to print out the file path and size, respectively.
Output
The specified file size has been shown in the above output.
Example 2: Get the File Size in “KiloBytes”
To convert the file size to “kilobytes”, we need to divide the file size in bytes by “1024”. Here’s an example code:
file_path =r'C:\Users\p\Documents\program\filename.txt'
file_size = os.path.getsize(file_path)
kb_size = file_size / 1024
print(f"The size of '{file_path}' is {kb_size:.2f} KB.")
In this code snippet:
- Firstly, get the file size in bytes using the “os.path.getsize()” method.
- In order to get the size in “kilobytes”, divide the file size by “1024”.
Output
Method 2: Get the File Size Utilizing “os.stat()” Method
In Python, you can also retrieve the file size by using the “os.stat()” method, which returns a tuple of file attributes, including the file’s size in bytes.
Example
Here is an example code:
file_path = r'C:\Users\p\Documents\program\filename.txt'
file_stat = os.stat(file_path)
file_size = file_stat.st_size
print(f"The size of '{file_path}' is {file_size} bytes.")
In the above code lines:
- The “os” module is again imported, but this time the “os.stat()” method is called having the file path as its parameter.
- This method creates a named tuple of file attributes, including the file size in bytes.
- The “st_size” attribute of the named tuple is accessed to determine the file size.
Output
It is shown above in the snippet how large the specified file is.
Method 3: Get the File Size Using “pathlib.Path().stat()” Method
In Python, you can also use the “pathlib.Path().stat()” method to calculate file sizes. It has the same functionality as “os.stat()”, but takes a path object in place of a string.
Example
Let’s overview the following code:
file_path = Path(r'C:\Users\p\Documents\program\filename.txt')
file_stat = file_path.stat()
file_size = file_stat.st_size
print(f"The size of '{file_path}' is {file_size} bytes.")
According to the above code:
- The “Path” class from the “pathlib” module is imported.
- The “Path” object is created with the file path as its argument and calls the “stat()” method on that object to get the file attributes.
- The “st_size” attribute of the named tuple is accessed to determine the file size.
Output
In this output, the size of the specified file is returned appropriately.
Conclusion
There are a variety of methods to determine the file size in Python such as the “os.path.getsize()”, “os.stat()”, or the “pathlib.Path().stat()” method. The “os.path.getsize()” method fetches the file size in bytes and kilobytes. The other methods can also be used to get the file size in various sizes. This Python post delivered various ways to get the Python file size.
from https://ift.tt/vN2ABdt
0 Comments