Examples
- How to Create Directory in Python? New and If Not Exists
- How to Change Directory in Python? Change Current Working Directory
- How to Get Current Working Directory in Python?
- How to Remove Directory in Python? (Empty & With Files)
- How to Copy Directory in Python? Program With Examples
- How to Open Directory in Python? Program & Example
- How to Read All Files in Directory in Python? (All Types of Files)
- How to Copy File in Python From One Directory to Another? (With and Without shutil)
- Python File Append Program (Append to File)
- How to Read Text File in Python Line by Line? (3 Ways)
- How to Get File Name From File Path in Python?
- Get Number of Lines in File in Python (Line Count)
- How to Get Size of File in Python? (File Size Program)
How to Copy File in Python From One Directory to Another? (With and Without shutil)
Learning how to copy file in Python is a basic skill for every programmer or developer, especially when handling files. It allows you to create duplicates of files, backup important data, or move/copy files from one directory to another. In this post, we will learn to copy a file in Python using different methods and libraries.
Python programming provides several approaches to copy files, ranging from simple file handling to using built-in libraries. Depending on your specific requirements, you can choose the most suitable method for your task. Let’s get started!
Copy Files From One Directory to Another in Python
The first method is to use the shutil module, which provides a high-level interface for file operations. Specifically, you can use the shutil.copy() or shutil.copy2() function to copy a file from one directory to another.
Code
import shutil
source_file = "/path/to/source/file.txt" # Path of the source file
destination_dir = "/path/to/destination/" # Path of the destination directory
# Use shutil.copy() to copy the file
shutil.copy(source_file, destination_dir)
Output
File copied successfully.
Explanation
In the code above, you need to replace "/path/to/source/file.txt" with the actual path of the file you want to copy, and "/path/to/destination/" with the destination directory where you want to copy the file.
This code will create a copy of the file in the specified destination directory. If you want to retain the original file name, you can provide the full path for the destination file instead of just the directory.
Copy File in Python Without shutil
Another way to copy a file from one directory to another in Python is by using the open() function to read the contents of the source file and the write() method to write the contents to the destination file.
Code
source_file = "/path/to/source/file.txt" # Path of the source file
destination_file = "/path/to/destination/file.txt" # Path of the destination file
# Open the source file in read mode
with open(source_file, 'r') as source:
# Read the contents of the source file
file_contents = source.read()
# Open the destination file in write mode
with open(destination_file, 'w') as destination:
# Write the contents to the destination file
destination.write(file_contents)
Output
File copied successfully.
Explanation
In this code, you need to replace "/path/to/source/file.txt" with the actual path of the file you want to copy and "/path/to/destination/file.txt" with the path of the destination file.
This method reads the contents of the source file and then writes them to the destination file.
It is important to note that this method only copies the contents of the file, not any additional metadata or permissions. If you need to preserve metadata or permissions, using the shutil module as shown in the previous example is recommended.