I have a scenario which i am trying to implement , where i need to check file is empty or not But need to consider below case statements
Case 1 : if file as empty lines and no records then message should be shown as File is Empty
Case 2 : if file as Only Spaces and no records then message should be shown as File is Empty
Case 3 : if file as Only tabs and no records then message should be shown as File is Empty
I have tried my below code , but not satisfying
My python code :
import os
def isEmp(fname):
with open(fname) as f:
f.seek(0, os.SEEK_END)
if f.tell():
f.seek(0)
else:
print('File is Empty')
Any approach to cover all above Cases 1,2,3
if not f.read().strip(): print('File is empty')if not f.read.strip(), a non empty string is truth value so an empty string is false.if not Truewhich isif False, the string is empty soprint('Empty file')