3

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.

Miikka
  • 4,573
  • 34
  • 47
joshlsullivan
  • 1,375
  • 2
  • 14
  • 21
  • 2
    What do you expect `list.remove(lines)` to do? `remove` is a method of the `list` type, so you have to call it on a particular list, not on `list` itself. – BrenBarn Jan 12 '15 at 07:09
  • I'm trying to print the first line, remove it, wait 60 seconds, and then print the 2nd line of the file. I want it to loop through until it get to the end of the txt file. – joshlsullivan Jan 12 '15 at 07:16
  • 1
    I think you should look at [the Python tutorial](https://docs.python.org/tutorial) and find some basic materials on how to work with files in Python. Your code is not looping over the lines in the file at all. Also, you have a `try` without any `except`, and your `while True` loop will always `break` on the first iteration. – BrenBarn Jan 12 '15 at 07:18
  • @BrenBarn This is my first stab at Python. I've looked through some tutorials but have found here much more helpful for explaining problems. – joshlsullivan Jan 12 '15 at 07:21
  • your question might have been more helpful for future readers if you had put the TypeError into the title and asked about printing lines with a delay as a separate question. – jfs Jan 12 '15 at 09:13
  • @joshlsullivan If my answer helps, don't forget to accept it so that others could find the answer quickly. Thanks! – Stephen Lin Jan 13 '15 at 07:37

5 Answers5

10

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)
Stephen Lin
  • 4,852
  • 1
  • 13
  • 26
  • 1
    This solution does not answer the problem: Read all lines with `readlines()` and then iterate over the lines you read. Doing it the way the above answer shows will create high disk IOPS and be slower than reading them all at once. – Wayne Workman Feb 15 '18 at 18:23
  • @WayneWorkman but often you want to loop through lines in a file that's bigger than working RAM so an iterator is much more memory efficient. – Ahmed Fasih Nov 29 '22 at 19:02
0

lines is a list here from your txt. files, and list.remove(lines) is not a correct syntax, you trying to delete a list on list. list is a function in Python. You can delete the elements in lines like;

del lines[0]
del lines[1]
...

or

lines.remove("something")

The logic is, remove() is deleting an element in a list, you have to write that list before remove() after then you have to write the thing that you want to delete in paranthesis of remove() function.

GLHF
  • 3,835
  • 10
  • 38
  • 83
0

Try this as per your requirements, this will do what you need.

import time
def main():
    with open("childrens-catechism.txt", "r") as file:
        for lines in file.readlines():
            if len(lines) > 0:
                for line in lines:
                    print line
                    lines.remove(line)
            else:
                print "No more lines to remove"
            time.sleep(60)
Prateek
  • 1,538
  • 13
  • 22
0

On opening a file, we can convert the file lines onto a list,

lines = list(open("childrens-catechism.txt", "r"))

From this list we can now remove entries with length greater than zero, like this,

for line in lines:
    if len(line) > 0:
            # do sth
            lines.remove(line)
elm
  • 20,117
  • 14
  • 67
  • 113
0

If you are trying to read all the lines from the file and then print them in order, and then delete them after printing them I would recommend this approach:

    import time

    try:
        file = open("childrens-catechism.txt")
        lines = file.readlines()

        while len(lines) != 0:
            print lines[0],
            lines.remove(lines[0])
            time.sleep(60)
    except IOError:
        print 'No such file in directory'

This prints the first line and then deletes it. When the first value is removed, the list shifts one up making the previous line (lines[1]) the new start to the list namely lines[0].

EDITED:

If you wanted to delete the line from the file as well as from the list of lines you will have to do this:

    import time

    try:
        file = open("childrens-catechism.txt", 'r+')  #open the file for reading and writing
        lines = file.readlines()

        while len(lines) != 0:
            print lines[0],
            lines.remove(lines[0])
            time.sleep(60)

        file.truncate(0) #this truncates the file to 0 bytes
    except IOError:
        print 'No such file in directory'

As far as deleting the lines from the file line for line I am not too sure if that is possible or efficient.

Loupi
  • 550
  • 6
  • 14
  • Does it delete the line from the file? – joshlsullivan Jan 12 '15 at 13:41
  • @joshlsullivan The topmost code only deletes the lines from the list of lines not from the file. The edited version truncates the file to 0 bytes; basically removing all the text in it. – Loupi Jan 12 '15 at 15:50