Question: In python I'm seeing evidence that fp.readlines() is closing the file when I try to access the fp later in the program. Can you confirm this behavior, do I need to re-open the file again later if I also want to read from it again? 

[Is the file closed?](https://stackoverflow.com/questions/16405926/is-the-file-closed) is similar, but didn't answer all of my questions.

```
import sys 

def lines(fp):
    print str(len(fp.readlines()))

def main():
    sent_file = open(sys.argv[1], "r")

    lines(sent_file)

    for line in sent_file:
        print line

```

this returns:

```
20

```

Answer: 
Once you have read a file, the file pointer has been moved to the end and no more lines will be 'found' beyond that point.

Re-open the file or seek back to the start:

```
sent_file.seek(0)

```

Your file is *not* closed; a closed file raises an exception when you attempt to access it:

```
>>> fileobj = open('names.txt')
>>> fileobj.close()
>>> fileobj.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

```
It doesn't close the file, but it does read the lines in it so they cannot be read again without reopening the file or setting the file pointer back to the beginning with `fp.seek(0)`.

As evidence that it doesn't close the file, try changing the function to actually close the file:

```
def lines(fp):
    print str(len(fp.readlines()))
    fp.close()

```

You will get the error:

```
Traceback (most recent call last):
  File "test5.py", line 16, in <module>
    main()
  File "test5.py", line 12, in main
    for line in sent_file:
ValueError: I/O operation on closed file

```