Solution 1

def copy_file(source_file, target_file):
    with open(source_file, "r") as s:
        read_data =
    with open(target_file, "w") as t:

Copy Content from One File to Another

Write a function that receives a path to two text files as parameters and copies
the content of the first file into the second, overwriting the content of the second
file if it's not empty.


copy_file('test-file.txt', 'copy.txt')

Test Cases

test copy file - Run Test

import tempfile

def test_copy_file():
    fp1 = tempfile.NamedTemporaryFile(mode="w")
    fp1.write('this is line 1\n')
    fp1.write('this is line 2\n')
    fp1.write('this is line 3\n')
    fp2 = tempfile.NamedTemporaryFile(mode="w")


    with open( as fp2:
        assert len(fp2.readlines()) == 3
        assert fp2.readlines()[2] == 'this is line 3\n'
def copy_file(source_file, target_file): pass