Python program to get size of any file

Write a python program to get size of any file with Practical Example

Program Logic:

  • Include os module in program using import statement
  • Ask user to enter any file name (text file,binary file)
  • Store it in variable fp
  • Use stat function of os module to get statistical detail of given file path
  • Pass file path as an argument to stat function and store it in variable size
  • Use st_size attribute to get size of file in bytes
  • Store it in another variable size_of_file
  • Display size of file in bytes using print function

Below is implementation code/Source code

import os
fp = input("Enter any file name :")
size = os.stat(fp)
size_of_file = size.st_size
print("File Size in bytes :",size_of_file)

Output:

%Run filesize.py
Enter any file name :book.txt
File Size in bytes : 170
%Run filesize.py
Enter any file name :student.dat
File Size in bytes : 183

Below is Snapshot of Executable code with output

<