-1

I have a BIG text file (100 000+ lines) from which I would like to have a loop that always opens the file, gets the first line of text (In this case it is a number), and then deletes that first line, so the next time the file is opened again, the first line is not the same. I have tried to do it so that the line number is always saved in a file and then it iterates to that specific line of the text file, but I believe this is not the most efficient way...

my text file consists of number like this:

1234
2212
3453
1232
... (and it goes on for ages)

The code I have attempted is:

with open('line_number.txt', 'rb') as inp:
line = inp.read()

fp = open("list.txt")

for i, line in enumerate(fp):
    if i == line - 1: #iterates until the number match 

        s = i + 1

        with open('new.txt', 'w') as outp:
            outp.write(s)
        outp.close()

        print(i)

    break
fp.close()

Is there a more efficient way to do this to large files? I believe iterating until the line I need is really time-inefficient...

George Lua
  • 17
  • 1
  • 5

1 Answers1

1

This would work if you need line number 1 to be removed... not if you need to find first occurence of a certain number.

with open('initial_file.txt', 'r') as file:
    lines = file.readlines()
    file.close()
line_that_you_need = lines[0]
with open('initial_file.txt', 'a') as file:
    for i in range(1, len(lines):
        file.write(lines[i])
    file.close
PythonSnek
  • 542
  • 4
  • 21