I am trying to remove the contents of the first given file with another file. This is because the first file contains data that is also in the second file. However the second file contains more and newer data than the first file. Hence the reason why I want to delete the content of the first file out of the second file.
I tried this:
# file 1 needs to be compared with file 2
with open(path8 "recent.txt") as timelog:
lines = timelog.read()
time = lines.split('\n', 1)[0]
print(time)
# file 1
finold = open(path7 time 'temp.csv', 'r')
# file 2
finnew = open(pathmain "file2.csv", 'r')
copyfinnew = open(path7 "tempfile1.csv", 'w')
fout = open(path7 "tempfile2.csv", 'w')
for line in finnew:
copyfinnew.write(line)
lines = line
delete = lines.split('\n', 1)[0]
if not delete in finold:
print(delete, " not in finold")
fout.write(delete '\n')
file 1 is a "log" file that is has been automatically saved from the last time the main code (not this) has run. file 2 is the new file that has yet to be run through the main code, but needs the contents of file 1 to be deleten from it so that the main code has less data to run through.
the error seems to be that the "if not delete in finold:" doesn't do what I anticipate it to do. I want the string from file 2 to go see if that same string exists in file 1. It does seem to loop correctly, however it returns all the strings from file 2. I have given an example of what I mean below.
I hope this made sense and I hope someone has an answer.
A sample for finold would be:
Srno,Flex Mood,Montana,08 Apr 2022 00:02
and for finnew it would be:
Bryan Mg,Bryan,Ça Sert à Rien,08 Apr 2022 00:11
Glowinthedark,Lituatie,Anders,08 Apr 2022 00:08
Architrackz,Perfecte Timing,Oh Baby (Whine),08 Apr 2022 00:05
Srno,Flex Mood,Montana,08 Apr 2022 00:02
that means that an example of fout would be:
Bryan Mg,Bryan,Ça Sert à Rien,08 Apr 2022 00:11
Glowinthedark,Lituatie,Anders,08 Apr 2022 00:08
CodePudding user response:
You are trying to iterate an the opened file buffer. You need to read them and split them into lines like you did in the first code block with timelog
if you want to iterate through each line.
Something like this:
# file 1
finold = open(path7 time 'temp.csv').read().split("\n")
# file 2
finnew = open(pathmain "file2.csv").read().split("\n")
copyfinnew = open(path7 "tempfile1.csv", 'w')
fout = open(path7 "tempfile2.csv", 'w')
for line in finnew:
copyfinnew.write(line)
if line not in finold:
print(line, " not in finold")
fout.write(line '\n')