如何在python中迭代readlines()

jos*_*van 2 python list readlines

我试图从txt文件添加行到python列表进行迭代,脚本想要打印每一行并返回错误.我正在使用readlines()函数,但是当我使用list.remove(lines)时,它会返回一个错误:File "quotes.py", line 20, in main list.remove(lines) TypeError: remove() takes exactly one argument (0 given).

def main():
while True:
    try:
        text_file = open("childrens-catechism.txt", "r")
        lines = text_file.readlines()
        #    print lines
        #    print len(lines)
        if len(lines) > 0:
            print lines
            list.remove(lines)
            time.sleep(60)
        else:
            print "No more lines!"
        break
        text_file.close()
Run Code Online (Sandbox Code Playgroud)

我看不出我做错了什么.我知道它与list.remove()有关.先感谢您.

Ste*_*Lin 8

你可以这样写.它会为您节省一些时间并提高效率.

import time
def main():
    with open("childrens-catechism.txt", "r") as file:
        for line in file:
            print line,
            time.sleep(60)
Run Code Online (Sandbox Code Playgroud)