سؤال

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

هل كانت مفيدة؟

المحلول

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)))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top