Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Iterate over readlines() in python

I am trying to add lines from a txt file to a python list for iteration, and the script wants to print every line and return an error. I'm using the readlines() function, but when I use list.remove(lines), it returns an error: 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()

I can't see what I'm doing wrong. I know it has to do with list.remove(). Thank you in advance.

like image 200
joshlsullivan Avatar asked Nov 21 '25 06:11

joshlsullivan


1 Answers

You can write in this way. It will save you some time and give you more efficiency.

import time
def main():
    with open("childrens-catechism.txt", "r") as file:
        for line in file:
            print line,
            time.sleep(60)
like image 150
Stephen Lin Avatar answered Nov 22 '25 19:11

Stephen Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!