Is there a way to check if a text file is empty in Python WITHOUT using os ?
What I have tried so far
x = open("friends.txt")
friendsfile = x.readlines()
if friendsfile == None
But I don't think it's the correct way.
Not sure why you wouldn't use os
, but I suppose if you really wanted you could open the file and get the first character.
with open('friends.txt') as friendsfile:
first = friendsfile.read(1)
if not first:
print('friendsfile is empty')
else:
#do something
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With