Python Copy Files From One Directory To Another
Last Updated : Aug 5, 2023
IN - Python
In this article we will show you the solution of python copy files from one directory to another, Python has several options for moving files around. Let's take a look at some of them.
You can also find a quick example of how shutil and OS libraries can be used to organize your downloads folder in the first section.
Those of you who need such a thing in their lives should keep reading.
We will learn how to copy files and folders between two locations using the OS, Shutil, and Subprocess modules in this Python tutorial.
We will now discuss the idea of how to copy files from one directory to another directory in python with an example.
Step By Step Guide On Python Copy Files From One Directory To Another :-
import os import shutil from shutil import SameFileError src_folder = r"E:\tc\files\reports" dst_folder = r"E:\tc\files\account" # printing the contents of the destination folder print("Destination folder before copying::", os.listdir(dst_folder)) # file names src_file = src_folder + "\test.txt" dst_file = dst_folder + "\test.txt" try: # copy file shutil.copyfile(src_file, dst_file) # destination folder after copying print("Destination after copying", os.listdir(dst_folder)) except SameFileError: print("We are trying to copy the same File") except IsADirectoryError: print("The destination is a directory")
- The operating system module can be imported using the import os command.
- The import shutil module is used to perform file operations with the system.
- A SameFileError exception has been imported from the Shutil module using import SameFileError.
- A file's source folder is specified in the variable src_folder = r"E:/tc/files/reports".
- This folder will be used for the destination file e:/tc/files/account.
- os.listdir(dst_folder) print("Destination folder before copying::", "Destination folder before copying") - printing contents of the destination folder before copying.
- The source file path is src_folder + "/test.txt" - the source file in the folder.
- The destination file path is dst_folder + "/test.txt" (replace with whatever you prefer).
- Make an attempt to copy the file with shutil by using the command "copy".
- This method copies the source file to the destination file using shutil.copyfile(src_file, dst_file).
- A print command prints the contents of the destination folder after copying it with the request print("Destination after copying", os.listdir(dst_folder)).
- Handles the SameFileError exception except for cases where the source and destination files are the same.
- The exception message is printed when you print ("We are trying to copy the same file").
- With the exception of IsADirectoryError:- handling a directory error if the destination is a directory.
- A message indicating that the destination is a directory will be printed by print("The destination is a directory").
As a result, we have successfully learned how to copy files from one directory to another directory in python with an example.
This is what you need to know! Now that you know how to copy files in Python using the shutil module and its methods, congratulations on your learning.
Here is a table that summarizes what each method does in order to help you choose which method to use.
I hope this article on python copy files from one directory to another helps you and the steps and the steps and method mentioned above are easy to follow and implement.