Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if text file is empty Python [duplicate]

Tags:

python

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.

like image 646
Lukaku Avatar asked Apr 15 '16 14:04

Lukaku


1 Answers

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
like image 66
miradulo Avatar answered Oct 21 '22 23:10

miradulo