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.

Updated on: 24-Aug-2023

38K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements