Attempting to read open file a second time gets no data
python
Solution
File objects are meant to be read once. Once `fin` has been `read()`, you can no longer read anything else from the file since it has already reached `EOF`.
To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ("").
http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects
If you really need reentrant access to your file use:
def lst():
fin.seek(0)
return fin.readlines()
Problem
``` 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?