CBSE Class 12 Computer Science Question 128 of 145

File Handling — Question 8

Back to all questions
8
Question

Question 8

Write a program that appends the contents of one file to another. Have the program take the filenames from the user.

Answer
def append_file(f1, f2):
    with open(f1, 'r') as source:
        with open(f2, 'a') as destination:
            destination.write(source.read())
    
source_file = input("Enter the name of the source file: ")
destination_file = input("Enter the name of the destination file: ")

append_file(source_file, destination_file)