- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to copy files from one folder to another using Python?
A file is a collection of information or data that is stored on a computer. You are already familiar with several file types, such as your audio, video, and text files.
Text files and binary files are the two categories into which we often split files. Simple text is contained in text files, as opposed to binary data, which can only be read by computers.
A group of files and subdirectories is called a directory or folder. A subdirectory is a directory present inside a directory. Numerous operating system functions can be carried out automatically.
File Operations Using Python
Python provides various methods to perform operations on the files and folders of the underlying operating system.
The OS module in Python has functions for adding and deleting folders, retrieving their contents, changing the directory, locating the current directory, and more. Import this module, we will use the listdir() method of it to fetch the files.
Similarly, the shutil module provides a number of functions for dealing with operations on files and associated collections. It gives users the option to copy and delete files. You can copy the contents of one folder to another using the shutil.copy(), shutil.copy2() and shutil.copytree() methods of this module.
You can include these functions in your file by importing their respective modules as shown below −
import shutil shutil.submodule_name(arguments passed)
Using shutil.copy() operation
Using this function, the text or content of the source file is copied to the target file or directories. Additionally, the permission mode of the file is preserved, but the file metadata (such as the “Date Creation”, “Date Modification” etc..) is not preserved.
Syntax
Following is the syntax of the shutil.copy() method−
shutil.copy(origin, target)
where
Origin − A string containing the source file's location or path
Target − A string containing the destination file's location or path.
Example
Following is an example of to copy files from one folder to other using shutil.copy() operation −
# importing the modules import os import shutil # Providing the folder path origin = 'C:\Users\Lenovo\Downloads\Works\' target = 'C:\Users\Lenovo\Downloads\Work TP\' # Fetching the list of all the files files = os.listdir(origin) # Fetching all the files to directory for file_name in files: shutil.copy(origin+file_name, target+file_name) print("Files are copied successfully")
Output
Following is an output of the above query:
Files are copied successfully
Note − Both a relative and an absolute path can be used to copy a file. The file's location on the disc is indicated by the path
The whole directory list needed to find the file is contained in an absolute path. For instance, an absolute path to find samples.txt is− C:\Users\Lenovo\Downloads\Works.
Here we are providing the folder path of both the source and the destination of the files.
Using shutil.copy2() operation
First of all, this function is exactly like copy() with the exception that it keeps track of the source file's metadata.
The execution program for this is exact same as shutil.copy(). The only difference is that while fetching the file to directory, in place of shutil.copy() we write shutil.copy2().
shutil.copy2(origin+file_name, target+file_name)
Syntax
Following is the syntax of the shutil.copy2() method –
shutil.copy2(origin, target)
Origin and target values are same as defined above.
The copy2() function in this code does one additional operation in addition to a copy() which is to keep the metadata.
Using shutil.copytree() method
This function moves a file and any subdirectories it contains from one directory to another.
This indicates that both the source and the destination include the file. The string must contain the names of both parameters.
Syntax
Following is the syntax of the shutil.copytree() method –
shutil.copytree(origin, target)
Origin and target values are same as defined above.
Example
Following is an example of to copy files from one folder to other using shutil.copytree() operation:
# importing the module import shutil # Fetching all the files to directory shutil.copytree('C:\Users\Lenovo\Downloads\Works\','C:\Users\Lenovo\Downloads\Work TP\/newfolder') print("File Copied Successfully")
Output
Following is an output of the above query:
File Copied Successfully
As an output we will be able to see the changes made after the execution i.e. the ‘Works’ folder gets copied to the ‘Works TP’ folder with the name of ‘newfolder’ as assigned in the code above containing all the files inside it which was there in the Works folder.
In order to obtain a duplicate of that file, we have included the copytree() function in this code.
- Related Articles
- How to copy certain files from one folder to another using Python?
- How to copy files from one server to another using Python?
- How to move a file from one folder to another using Python?
- How can I copy a file from one folder to another folder within a container in Docker?
- How to Copy files or Folder without overwriting existing files?
- How to copy items from one location to another location using PowerShell?
- How we can copy Python modules from one system to another?
- How to copy Docker images from one host to another without using a repository?
- How to copy files to a new directory using Python?
- How to copy a table from one MySQL database to another?
- How to copy rows from one table to another in MySQL?
- How to read multiple text files from a folder in Python?(Tkinter)
- How to copy the palette from one image to another using imagepalettecopy() function in PHP?
- How to copy a collection from one database to another in MongoDB?
- Copy values from one array to another in Numpy
