Python copy file from a source to destination


To copy a file in Python for a source location to a destination, you can use the shutil module.


Example (macOS/Linux):
import shutil

source_path = "/user/c2c/files/myfile.txt"
destination_path = "/user/c2c/archives/myfile.txt"

# copy files from source to destination using shutil.copy
shutil.copy(source_path, destination_path)

Example (Windows):
import shutil

source_path = "D:/c2c/files/myfile.txt"
destination_path = "D:/c2c/archives/myfile.txt"

shutil.copy(source_path, destination_path)


Note: the file to be copied to the destination path already exists, shutil.copy will overwrite it.

Note: If the source or destination file does not exists, you will get FileNotFoundError.

Traceback (most recent call last):
  File "/Users/c2ctech/Desktop/example.py", line 7, in <module>
    shutil.copy(source_path, destination_path)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/shutil.py", line 418, in copy
 ..
 ..
FileNotFoundError: [Errno 2] No such file or directory: '/user/c2c/files/myfile.txt'
Traceback (most recent call last):
  File "/Users/c2ctech/Desktop/example.py", line 7, in <module>
    shutil.copy(source_path, destination_path)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/shutil.py", line 418, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/shutil.py", line 264, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: '~/Desktop/archives/myfile.txt'

In order to copy the file with a new name, you can specify the new file name in the destination path. For example, if you want to copy myfile.txt to the destination directory with a new name archived_myfile.txt, follow the below code.

import os
import shutil

source_path = "D:/c2c/files/myfile.txt"
destination_path = "D:/c2c/archives/archive_myfile.txt"

# Create the destination directory if it does not exist
os.makedirs(os.path.dirname(destination_path), exist_ok=True)

# Copy the file to destination
copied_path = shutil.copy(source_path, destination_path)

print(f"file copied to: {copied_path}")
Output:

file copied to: D:/c2c/archives/archive_myfile.txt

The method returns the path to the newly created file.

copy() copies the file data and the file’s permission mode, metadata, such as the file’s creation and modification times, is not preserved. To preserve all file metadata from the original, use copy2() instead.

copied_path = shutil.copy2(source_path, destination_path)
Copy file from source to destination using copy and copy2 methods

Reference

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap