Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
# Write a Python program to get the file size of a plain file
import os
file= input("Enter file name: ") 
try:  
    with open(file) as f:
       size=os.path.getsize(f)
       print("File Size is :",f, "bytes")
except:
    print('File cannot be opened:', file)


What I have tried:

without try and except block following error occurs...
Exception has occurred: TypeError
stat: path should be string, bytes, os.PathLike or integer, not TextIOWrapper
Posted
Updated 1-Jan-22 8:02am
v3

1 solution

The error message is pretty obvious. You're passing in a file stream, not what the getSize function is looking for. You don't have to open the file to get its size.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900