1

Beginner Python user here. I'm trying to write a formula that will merge two text files. As in, the first file should have the text of the second simply added to it, not replacing anything in either file.

Here's what I have so far:

def merge(file1,file2):
    infile = open(file2,'r')
    infile.readline()
    with open('file1','a') as myfile:
        myfile.write('infile')
        myfile.close()

Any help would be greatly appreciated.

alyssaeliyah
  • 2,214
  • 6
  • 33
  • 80
ebroker
  • 27
  • 3
  • are you trying to merge or just copy content from file2=>file1 or file1=file1+file2? – brokenfoot Feb 06 '20 at 04:31
  • Does this answer your question? [Copying from one text file to another using Python](https://stackoverflow.com/questions/15343743/copying-from-one-text-file-to-another-using-python) – Hymns For Disco Feb 06 '20 at 04:31

1 Answers1

3

You seem to have the right idea, a way it could be simplified and easier to read would be the following code I found on google, fit to your method.

def merge(file1,file2):
    fin = open(file2, "r")
    data2 = fin.read()
    fin.close()
    fout = open(file1, "a")
    fout.write(data2)
    fout.close()