In this python file program, we will get the size of a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.
#readcontent.txt
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.
Steps to solve the program
- Import the os library.
- Open the file using os.stat(‘readcontent’) and store the output in the variable.
- Get the size of the file using info.st_size.
- Print the output.
# Import os library
import os
# Read file
info = os.stat('readcontent.txt')
# Print size of the file
print("Size of the file: ",info.st_size)
Output:
Size of the file: 193