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)
fin.flush()
In above code when print lst() is called outside function it gives correct result, but when trying to call same function in function foo() it produces empty list which makes len(lst()) value 0. I also tried by commenting last two line but still it gives back empty list. What is wrong in above code?
openingfinoutsidelst()? - Johnsywebwithstatement for opening files in Python. It's more readable and ensures your file is closed, even if an exception is thrown. - Gareth Latty