Editor

Python Program to Copy the Contents of a File

Beginner · File handling

Copying a file means reading everything from one file, the source, and writing it to another, the destination. This program copies a text file line by line, prints the copy, and then compares the two files to check that the copy is exact.

Program

copy_file.py
# Make a sample file to work on
with open("original.txt", "w") as f:
f.write("Line one of the original file\n")
f.write("Line two has some numbers: 10, 20, 30\n")
f.write("Line three is the last line\n")
source = "original.txt"
destination = "copy.txt"
# Read each line from one file and write it to the other
with open(source, "r") as src, open(destination, "w") as dst:
for line in src:
dst.write(line)
print("Copied", source, "to", destination)
with open(destination, "r") as f:
print("Contents of", destination + ":")
print(f.read(), end="")
with open(source, "r") as a, open(destination, "r") as b:
print("The two files are identical:", a.read() == b.read())
Output
Copied original.txt to copy.txt
Contents of copy.txt:
Line one of the original file
Line two has some numbers: 10, 20, 30
Line three is the last line
The two files are identical: True

How it works

  1. The program writes a three-line file first, so there is something to copy.
  2. One with statement can open two files at once: src for reading and dst for writing.
  3. for line in src reads one line at a time and dst.write(line) writes it unchanged, newline included, so even a very large file is copied without loading all of it.
  4. Reading both files again and comparing their text with == confirms the copy is exact.

Other ways to write it

Using shutil.copyfile()

Using shutil.copyfile()
import shutil
# Make a sample file to work on
with open("original.txt", "w") as f:
f.write("Copied with one function call\n")
shutil.copyfile("original.txt", "backup.txt")
with open("backup.txt", "r") as f:
print("backup.txt says:", f.read(), end="")
Output
backup.txt says: Copied with one function call

shutil.copyfile() from the standard library copies any file, text or binary, in one line. Use it in real programs, and the loop when your teacher asks you to read and write the file yourself.

Copying a binary file

Copying a binary file
# Make a sample file to work on (raw bytes, like an image holds)
with open("data.bin", "wb") as f:
f.write(bytes([0, 1, 2, 3, 250, 255]))
with open("data.bin", "rb") as src, open("data_copy.bin", "wb") as dst:
dst.write(src.read())
with open("data_copy.bin", "rb") as f:
print("Bytes in the copy:", list(f.read()))
Output
Bytes in the copy: [0, 1, 2, 3, 250, 255]

Opening both files in binary mode, "rb" and "wb", copies the bytes exactly as they are, which is how you copy images, PDFs and other files that are not plain text.

Try it yourself

Change the loop so it writes line.upper() instead of line, making a copy in capital letters.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises