Python file program to get the size of a file

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
  1. Import the os library.
  2. Open the file using os.stat(‘readcontent’) and store the output in the variable.
  3. Get the size of the file using info.st_size.
  4. 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

 

 

count the number of lines in a file.

write a tuple to a file.

Leave a Comment