Compare two text files in python and print the difference in third file
Below is the code which takes 2 text files as a input and it will generate the difference(in csv or comma separated file) and finally print the difference in the third file. I have developed this for excel like record file.
The code works for as many numbers of records or huge text data. Also the program execution is very faster.
Here is the code:
import os
# Read in the original and new file
old = open(OLD_FILE_PATH,'r')
new = open(NEW_FILE_PATH,'r')
#in new but not in old
new_extra = set(new) - set(old)
# To see results in console if desired
print('-------------------------------------------new file extra records------------------------------------------------------------')
print(new_extra)
# Write to header to csv file
with open(NEW_FILE_PATH) as f1:
first_line1 = f1.readline()
with open(NEW_FILE_PATH, 'a') as file_out:
file_out.write(first_line)
# write to output files
with open(NEW_FILE_PATH+'/DIFFERENCE.csv', 'a') as file_out2:
for line2 in new_extra:
file_out2.write(line2)
#close the files
old.close()
new.close()
new_extra.close()
Below is the code which takes 2 text files as a input and it will generate the difference(in csv or comma separated file) and finally print the difference in the third file. I have developed this for excel like record file.
The code works for as many numbers of records or huge text data. Also the program execution is very faster.
Here is the code:
import os
# Read in the original and new file
old = open(OLD_FILE_PATH,'r')
new = open(NEW_FILE_PATH,'r')
#in new but not in old
new_extra = set(new) - set(old)
# To see results in console if desired
print('-------------------------------------------new file extra records------------------------------------------------------------')
print(new_extra)
# Write to header to csv file
with open(NEW_FILE_PATH) as f1:
first_line1 = f1.readline()
with open(NEW_FILE_PATH, 'a') as file_out:
file_out.write(first_line)
# write to output files
with open(NEW_FILE_PATH+'/DIFFERENCE.csv', 'a') as file_out2:
for line2 in new_extra:
file_out2.write(line2)
#close the files
old.close()
new.close()
new_extra.close()
Comments
Post a Comment