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)

Reference
Have Questions? Post them here!
- tkinter - Hello World! Program
- How to install Python Specific version (3.8, 3.9 or 3.10) using Brew
- How to install SpaCy (NLP Library) on Mac
- Python matplotlib segmentation fault: 11 macOS Big Sur
- How to uninstall pip Python packages
- 3 Ways to find if element is present in a List in Python
- How to Convert Python String to DateTime Object
- Python f-strings Formatted String Literals Syntax and Examples
- Where does brew install python in macOS
- Take input argument from command line in Python Programming
- Advanced print() Function Tutorial and Techniques for Python Developers
- How to add borders to tkinter label text
- Whats new in Python 3.10 Pre-release
- Float built-in function in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- How to check if Key Exists in Python Dictionary?
- Read a file line by line in Python Program
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- What is the Max and Minimum Value of int type in Python?
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- Fix: TypeError: can only concatenate str (not int) to str in Python
- How to take user input from the console in a Python program
- [Fix] TypeError: str object is not callable in Python
- 3 Python program to add two numbers
- How to delete a file using Python code example
- How to run a Maven jar project from eclipse without tomcat - Eclipse
- StringTokenizer in Java with Examples - Java
- Open Docker Desktop from macOS Terminal - Docker
- SharePoint Open in the client application document opens in browser - Java
- Pretty Print JSON String in Java Console Output - Java
- Microsoft Office Mac Ventura: System Settings must be changed before Microsoft AutoUpdate can run - Microsoft
- How to open SharePoint Online Modern SPFX links in new tab - SharePoint
- Call a Stored Procedure using Java JDBC CallableStatement Example - Java