Copy a file

EasyOS Module Programs · 17 of 49

Write a Python program to copy a file from a source path to a target path.

Advertisement
Output
quarterly report

Approach

  1. shutil.copy copies data and permissions; copy2 also copies timestamps.

Solution

Python
import osimport shutil os.makedirs("src", exist_ok=True)os.makedirs("dst", exist_ok=True)with open("src/report.txt", "w") as f:    f.write("quarterly report")shutil.copy2("src/report.txt", "dst/report.txt")print(open("dst/report.txt").read())
Output
quarterly report

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Leave a Comment