Python Program - Check the File Size
here's a Python program that checks the size of a file:
ref reto:theitroad.com# import os module import os # get the file path from user file_path = input("Enter the file path: ") # get the size of the file in bytes file_size = os.path.getsize(file_path) # print the file size in bytes and kilobytes print("File size: {} bytes".format(file_size)) print("File size: {:.2f} KB".format(file_size/1024))
In this program, we first import the os
module, which provides a way to interact with the operating system.
We then use the input()
function to get the file path from the user, and store it in the variable file_path
.
We use the os.path.getsize()
function to get the size of the file in bytes, and store the result in the variable file_size
.
Finally, we use the print()
function to print out the file size in bytes and kilobytes, by dividing the file size in bytes by 1024 to get the file size in kilobytes. We use the string format method to format the output with two decimal places using :.2f
.