All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Copy Files From One Directory To Another

Last Updated : Mar 11, 2024

Python Copy Files From One Directory To Another

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")
  1. The operating system module can be imported using the import os command.
  2. The import shutil module is used to perform file operations with the system.
  3. A SameFileError exception has been imported from the Shutil module using import SameFileError.
  4. A file's source folder is specified in the variable src_folder = r"E:/tc/files/reports".
  5. This folder will be used for the destination file e:/tc/files/account.
  6. os.listdir(dst_folder) print("Destination folder before copying::", "Destination folder before copying") - printing contents of the destination folder before copying.
  7. The source file path is src_folder + "/test.txt" - the source file in the folder.
  8. The destination file path is dst_folder + "/test.txt" (replace with whatever you prefer).
  9. Make an attempt to copy the file with shutil by using the command "copy".
  10. This method copies the source file to the destination file using shutil.copyfile(src_file, dst_file).
  11. A print command prints the contents of the destination folder after copying it with the request print("Destination after copying", os.listdir(dst_folder)).
  12. Handles the SameFileError exception except for cases where the source and destination files are the same.
  13. The exception message is printed when you print ("We are trying to copy the same file").
  14. With the exception of IsADirectoryError:- handling a directory error if the destination is a directory.
  15. A message indicating that the destination is a directory will be printed by print("The destination is a directory").

Conclusion :-

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.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪