Reading line by line from a file in python

This question already has an answer here:

  • In Python, how do I read a file line-by-line into a list? 35 answers

  • You need to iterate through the file rather than the line:

    #! /usr/bin/python
    file = open('/home/results/err.txt')
    for line in file:
        print line
    

    file.readline() only reads the first line. When you iterate over it, you are iterating over the characters in the line.


    file.readline() already reads one line. Iterating over that line gives you the individual characters.

    Instead, use:

    for line in file:
        …
    

    尝试这个 :

    #! /usr/bin/python
    file = open('/home/results/err.txt')
    for line in file.readlines():
        print line
    
    链接地址: http://www.djcxy.com/p/42356.html

    上一篇: pythons相当于PHP的文件(fn,FILE

    下一篇: 从python文件中逐行读取