File Copy

Copy the contents of one file to another.

BeginnerTopic: File Handling Programs
Back

Python File Copy Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# 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

Understanding File Copy

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

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents