1

I'm opening two files - one that contains the new file for comparison and one that contains the buzz words I need to remove from this file. I have this as a function so far:

def remove(file, buzz):
    #outer for loop cycles through the buzz file
    for line in buzz:
        #inner for loop cycles through the new file
        for line2 in file:
            if (line==line2):
                file.remove(line2)
            else:
                continue

where file is the new file that has been opened in main() and being passed to this, and buzz is the buzz file that is being opened and passed from main().

The removal section is not working and the new file does not change.

Any suggestions?

martineau
  • 119,623
  • 25
  • 170
  • 301
user131935
  • 11
  • 3

2 Answers2

3

First read the contents of each file and put them in lists:

a_list = open(file_a).read().splitlines()
b_list = open(file_b).read().splitlines()

Then remove the words in a_list that are in b_list:

a_list = [word for word in a_list if word not in b_list]

a_list now contains only the words that are not in b_list

Degraw
  • 513
  • 5
  • 10
0

Open and put file data into lists:

file_data = [line.strip() for line in open(file)]
buzz_data = [line.strip() for line in open(buzz)]

Then filter out the words:

new = [line for line in file_data if line not in buzz_data]

Finally write the new data to the file:

with open(file,"w") as f:
    for i in new:
        f.write(i+"\n")
f.close()