fin = open(\'/abc/xyz/test.txt\', \'a+\')
def lst():
return fin.read().splitlines()
print lst()
def foo(in):
print lst()
fin.write(str(len(lst()) + in)
fi
Once you read a complete file into memory, reading some more from that file will result in an empty string being returned:
>>> example = open('foobar.txt')
>>> example.read()
'Foo Bar\n'
>>> example.read()
''
In other words, you have reached the end of the file. You have three alternatives here:
Use .seek() to go to the start of the file again:
>>> example = open('foobar.txt')
>>> example.read()
'Foo Bar\n'
>>> example.seek(0)
>>> example.read()
'Foo Bar\n'
Store the contents of the file in a variable, thus caching it in memory, then use that instead of re-reading the file.