I have the following function in python. It's a simple function that reads a file. I coded it to act differently when the file is empty. When the file is non-empty, len(f.read())
has a non zero value but still it gets into the if condition.
Code:
def data():
with open('data.dat','r') as f:
print (len(f.read()))
print(len(f.read())==0)
if len(f.read())==0:
print ("IF Entered")
else:
print ("Else Entered")
return {}
Output:
19
True
IF Entered
Expected Output:
19
False
Else Entered
EDIT:
I updated my code(I wanted to use the f
twice for json.load
but when I used it once, it went to the end to give crazy outputs. Now this gets the work done.) to:
def data():
with open('data.dat','r') as f:
if len(f.read())==0:
return {'post':{}}
with open('data.dat','r') as f:
return json.load(f)