python looping through input file

后端 未结 4 1353
抹茶落季
抹茶落季 2020-12-16 15:07

My question is related to file-input in Python, using open(). I have a text file mytext.txt with 3 lines. I am trying to do two things with this fi

4条回答
  •  自闭症患者
    2020-12-16 15:21

    The file handle is an iterator. After iterating over the file, the pointer will be positioned at EOF (end of file) and the iterator will raise StopIteration which exits the loop. If you try to use an iterator for a file where the pointer is at EOF it will just raise StopIteration and exit: that is why it counts zero in the second loop. You can rewind the file pointer with input_file.seek(0) without reopening it.

    That said, counting lines in the same loop is more I/O efficient, otherwise you have to read the whole file from disk a second time just to count the lines. This is a very common pattern:

    with open('filename.ext') as input_file:
        for i, line in enumerate(input_file):
            print line,
    print "{0} line(s) printed".format(i+1)
    

    In Python 2.5, the file object has been equipped with __enter__ and __exit__ to address the with statement interface. This is syntactic sugar for something like:

    input_file = open('filename.txt')
    try:
        for i, line in enumerate(input_file):
            print line,
    finally:
        input_file.close()
    print "{0} line(s) printed".format(i+1)
    

    I think cPython will close file handles when they get garbage collected, but I'm not sure this holds true for every implementation - IMHO it is better practice to explicitly close resource handles.

提交回复
热议问题