Attempting to read open file a second time gets no data

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()

Leave a Comment