1

I'm making a program which deletes certain lines from an existing file. It takes file1 as entry(f1), it looks for a certain pattern and if it finds it, it modifies the line (to make it compatible with the other file) and saves this modification in a variable 'mark'. It opens another file f2, and searches 'mark' in it. If it finds 'mark' in a certain line in f2, I have to delete that line and the three lines after. The thing is that when I run it, the program deletes everything from f2, so I get an empty file as a result.

new=''
pattern2 = '2:N:0:8'
i=0


f1=open('test_reverse.txt','r')
for line in f1:
    if pattern2 in line:
        mark=line.replace('2:N:0:8','1:N:0:8')
        f2=open('test_OKforward2.txt','r')
        lines=f2.readlines()
        for i, line in enumerate(lines):
            if mark in lines[i]:
                e=lines[i]
                e1=lines[i+1]
                e2=lines[i+2]
                e3=lines[i+3]
                new=e+e1+e2+e3
            f3=open('test_OKforward2.txt','w')
            if line!=new:
                f3.write(line)

I tried with the next() function as well, but I got the same result and a 'stop iteration' error.

Luc
  • 9
  • 5
  • Please remember to come back and accept the answer your found most useful for you. It benefits you, those trying to answer your question, and the community at large. That's how we say 'thank you' around here. – code_dredd Nov 30 '15 at 00:51

1 Answers1

0

The thing is that when I run it, the program deletes everything from f2, so I get an empty file as a result.

Whenever you open a file for writing, everything in it is lost. You have to re-write everything you wish to preserve in the files and exclude what you wanted to delete in the first place.

Notice these lines:

f2=open('test_OKforward2.txt','r')
# ...
f3=open('test_OKforward2.txt','w')

The problem is that f3 is opening the same file as f2 for writing for every loop on the lines of file f2.

Basically, after you add lines, you re-open the file for writing, eliminating what you had previously.

First: You should remove the f3=open from within the loop iterating on each line of f2 (i.e. do this at some other location outside this loop). This is the main issue.

Second: Use a temporary file for the process instead and, at the end, rename the temporary file to the one you had.

Third: You're not closing the files. Consider using context managers. Your code would look more like this:

with open('something.txt') as f2:
    # do something with f2;
    # f2 with be automatically closed when it exits the ctx manager

Fourth: Follow the PEP-8 style standards for your code. Everyone reading your code will thank you.

I got [...] a 'stop iteration' error.

This is normal; you said you were using the next() function. Iterators and next() raise StopIteration in order to signal that they cannot produce more elements from the collection being iterated and that this iteration process should stop.

Quoting the docs:

exception StopIteration

Raised by built-in function next() and an iterator‘s __next__() method to signal that there are no further items produced by the iterator.

code_dredd
  • 5,915
  • 1
  • 25
  • 53