Oscar Benjamin <oscar.j.benja...@gmail.com> Wrote in message: > On 17 February 2014 13:16, Dave Angel <da...@davea.name> wrote: >> On 02/17/2014 06:12 AM, Oscar Benjamin wrote: >>> >>> Something like this: >>> >>> with open(r'D:\file3.txt', 'r+') as fout: >>> keywords_seen = set() >>> for filename in r'C:\File1.txt', r'C:\File2.txt': >>> with open(filename) as fin: >>> for line in fin: >>> keyword = line.strip() >>> if keyword not in keywords_seen: >>> fout.write(line) >>> keywords.add(keyword) > > The line above should obviously be keywords_seen.add(keyword) > >> You forgot to append the newline to strings in the write() method calls. >> >> fout.write(line + "\n") > > Iterating over a file keeps the newlines: > >>>> list(open('file1.txt')) > ['file1\n', 'file2\n'] >
Sorry, I read your code too quickly and figured you had done an in-place strip(). Since you did not, you run the risk of encountering a file without trailing newline. I prefer to at least rstrip each line, then add the newline back in when writing. -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor