0

I have 2 different text files.

File1.txt contains:

program
RAM
Python
parser

File2.txt contains:

file
1234
program

I want to perform an union of these both files such that i get an output file3.txt which contains:

program
RAM
Python
parser
file
1234

Is it possible to program this in python? I use python 2.7

Ray
  • 2,472
  • 18
  • 22
Aaron Misquith
  • 621
  • 1
  • 7
  • 11

1 Answers1

0
with open("File1.txt") as fin1: lines = set(fin1.readlines())
with open("File2.txt") as fin2: lines.update(set(fin2.readlines()))
with open("file3.txt", 'w') as fout: fout.write('\n'.join(list(lines)))
GVH
  • 416
  • 3
  • 16
  • It doesn't perform an union. It just combines the two files. I don't want the 'program' keyword to be repeated again. – Aaron Misquith Feb 17 '14 at 08:07
  • Yes, I realized that and fixed it within a minute or so of posting. The current version uses a `set()` object. – GVH Feb 17 '14 at 10:11