CBSE Class 12 Computer Science Question 127 of 145

File Handling — Question 7

Back to all questions
7
Question

Question 7

Write a program that copies one file to another. Have the program read the file names from user ?

Answer
def copy_file(file1, file2):
    with open(file1, 'r') as source:
        with open(file2, 'w') 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: ")

copy_file(source_file, destination_file)