File Copy

Copy the contents of one file to another.

PythonBeginner
Python
# Program to copy a text file

src = input("Enter source filename: ")
dst = input("Enter destination filename: ")

try:
    with open(src, "r", encoding="utf-8") as f_src, open(dst, "w", encoding="utf-8") as f_dst:
        for line in f_src:
            f_dst.write(line)
    print("File copied from", src, "to", dst)
except FileNotFoundError:
    print("Source file not found.")

Output

Enter source filename: input.txt
Enter destination filename: backup.txt
File copied from input.txt to backup.txt

Opens source and destination files simultaneously and streams line by line.