Copy a File to Another Location using Python

In Python, you can copy a file to another location using the shutil module, which provides a function called shutil.copy() or shutil.copy2(). These functions allow you to duplicate a file from one directory to another efficiently while preserving the file’s content.


Examples to Copy a File to Another Location

1. Copy a File to Another Directory Using shutil.copy()

In this example, we will copy a file named source.txt from the current directory to a folder named backup using the shutil.copy() function.

main.py

</>
Copy
import shutil

# Define source and destination file paths
source_file = "source.txt"
destination_folder = "backup/"
destination_path = destination_folder + "source_copy.txt"

# Copy the file
shutil.copy(source_file, destination_path)

print("File copied successfully!")

Explanation:

  1. We import the shutil module, which provides file copy functionalities.
  2. Define the source_file as "source.txt", which is the file we want to copy.
  3. Set destination_folder as "backup/", where we want to copy the file.
  4. Define the complete destination path, including the filename, using destination_path.
  5. Use shutil.copy(source_file, destination_path) to copy the file.
  6. Print a success message confirming that the file was copied.

Output:

2. Copy a File While Preserving Metadata Using shutil.copy2()

In this example, we will use shutil.copy2(), which not only copies the file but also retains metadata such as timestamps.

main.py

</>
Copy
import shutil

# Define source and destination file paths
source_file = "data.txt"
destination_file = "backup/data_copy.txt"

# Copy the file along with metadata
shutil.copy2(source_file, destination_file)

print("File copied successfully with metadata!")

Explanation:

  1. Import the shutil module.
  2. Specify source_file as "data.txt", the file to be copied.
  3. Set destination_file as "backup/data_copy.txt", the target location.
  4. Use shutil.copy2(source_file, destination_file) to copy the file along with metadata.
  5. Print a confirmation message indicating that the file was copied with metadata.

Output:

File copied successfully with metadata!

3. Copy a File and Rename It in the New Location

In this example, we will copy a file to a new directory and rename it at the same time.

main.py

</>
Copy
import shutil

# Define source and new destination file paths
source_file = "report.pdf"
destination_file = "documents/report_backup.pdf"

# Copy and rename the file
shutil.copy(source_file, destination_file)

print("File copied and renamed successfully!")

Explanation:

  1. Import the shutil module.
  2. Define source_file as "report.pdf", the file to be copied.
  3. Set destination_file as "documents/report_backup.pdf", renaming the copied file.
  4. Use shutil.copy(source_file, destination_file) to copy and rename the file.
  5. Print a success message confirming the copy operation.

Output:

File copied and renamed successfully!

4. Copy a File Using os Module

The os module provides another way to copy a file using os.system() with shell commands.

main.py

</>
Copy
import os

# Define source and destination paths
source_file = "image.jpg"
destination_file = "backup/image_copy.jpg"

# Copy using shell command
os.system(f"cp {source_file} {destination_file}")

print("File copied using os module!")

Explanation:

  1. Import the os module.
  2. Define source_file as "image.jpg", the file to copy.
  3. Set destination_file as "backup/image_copy.jpg", the new location.
  4. Use os.system() with the shell command cp to copy the file.
  5. Print a message confirming that the file was copied.

Output:

File copied using os module!

Conclusion

Python provides multiple ways to copy a file to another location:

  1. Using shutil.copy() for a basic file copy.
  2. Using shutil.copy2() to retain metadata.
  3. Using os.system() for shell command-based copying.