I have been trying to compare two python files for differences in them.
file_a.py
some_var=<a line of code>
# a coment
x = some_var
file_b.py
ssome_var=<a line of code>
# a comment
x = some_var
Now I want to do something like this:
Extract the lines from both files that do not have an instance in either of them and save the result to a 3rd file, and while writing the 3rd file, assign the source of the extracted line (i.e. the file name).
This is what I have tried which is generating an output file with the correct set of lines which do not have an instance in either of them, but I am unable to kind of put an *address to those line/s.
f1='file_a.py'
f2='file_b.py'
with open(f1, 'r') as file1:
with open(f2, 'r') as file2:
diff = set(file1).symmetric_difference(file2)
# Ascribe each item found to its respective source file
result = dict([(i, ('f1' if i in file1 else 'f2')) for i in diff])
with open('comp_diff.py', 'w') as file_out:
for line in result:
file_out.write(line)
Output file `comp_diff.py
# a comment
some_var=<a line of code>
# a coment
ssome_var=<a line of code>
Is there a way I can assign the addresses from where the lines were extracted from?
PS. Another point here. If I take a print of the var "result" in the output (in py shell) I get something like:
{'# a comment\n': 'f2', 'some_var=<a line of code>\n': 'f2', ' # a coment\n': 'f2', 'ssome_var=<a line of code>\n': 'f2'}
That is, all the values are marked as from the file f2!