3

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? 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
Community
  • 1
  • 1
mwmath
  • 69
  • 2
  • 9
  • It doesn't close a file, but it does read all of the lines in it (so they can't be read again unless you reopen the file. – David Robinson May 12 '13 at 14:16
  • 6
    It's worth noting that when working with files in Python, it's good practice to use [the `with` statement](http://www.youtube.com/watch?v=lRaKmobSXF4). – Gareth Latty May 12 '13 at 14:20
  • 1
    `print fp.closed` tells you if it is closed or not – georg May 12 '13 at 14:47
  • 1
    The `seek` method can also be used to reset the file pointer to some place earlier than the end of the file, if the file object is seekable (typically, real files are seekable, pipelines are not). – chepner May 12 '13 at 15:41

4 Answers4

10

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
3

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
David Robinson
  • 77,383
  • 16
  • 167
  • 187
1

It won't be closed, but the file will be at the end. If you want to read its contents a second time then consider using

f.seek(0)
Tom Dalton
  • 6,122
  • 24
  • 35
0

You may want to use the with statement and context manager:

>>> with open('data.txt', 'w+') as my_file:     # This will allways ensure
...     my_file.write('TEST\n')                 # that the file is closed.
...     my_file.seek(0)
...     my_file.read()
...
'TEST'

If you use a normal call, remember to close it manually (in theory python closes file objects and garbage collect them as needed):

>>> my_file = open('data.txt', 'w+')
>>> my_file.write('TEST\n')   # 'del my_file' should close it and garbage collect it
>>> my_file.seek(0)
>>> my_file.read()
'TEST'
>>> my_file.close()     # Makes shure to flush buffers to disk
HarmonicaMuse
  • 7,633
  • 37
  • 52