Copy all files from one directory to another using Python
Last Updated :
26 Apr, 2025
Copying files from one directory to another involves creating duplicates of files and transferring them from one folder to another. This is helpful when organizing files, backing them up, or moving them to different locations on your computer. Let’s explore various methods to do this efficiently.
Using shutil.copytree()
shutil.copytree() copy an entire directory, including all its files and subdirectories, to a new location. This method recursively copies the entire directory tree, creating the destination folder if it doesn’t exist. It is ideal when you need to replicate the structure of a directory. Directory in use:
fol1
Python
import shutil
import os
s = 'fol1' # source directory
d = 'fol2' # destination directory
if not os.path.exists(d):
shutil.copytree(s, d)
else:
print("Already exists")
Output
fol 2Explanation: This code checks if fol2 exists using os.path.exists(d). If it doesn't exist, it uses shutil.copytree(s, d) to copy all contents of fol1 to fol2. If fol2 already exists, it prints "Already exists" to prevent overwriting.
Using shutil.copy2()
shutil.copy2() copies individual files and preserves the file's metadata (like timestamps and permissions), unlike shutil.copy() which doesn't retain such metadata. Directory in use:
Python
import shutil
import os
s = 'source'
t = 'destination'
files=os.listdir(s)
for fname in files:
shutil.copy2(os.path.join(s,fname), t)
Output

Explanation: os.listdir(s) retrieves a list of files and directories in the source directory, storing them in the files variable. The for loop iterates over each file name (fname) and os.path.join(s, fname) creates the full path. shutil.copy2() then copies each file to the destination directory, preserving its metadata.
Using shutil.copy()
If you don't need to preserve metadata (like timestamps) or copy entire directory structures recursively, you can use a simpler method with os and shutil.copy() for individual files. Directory in use:
Python
import shutil
import os
s = 'source'
t = 'destination'
files = os.listdir(s)
for fname in files:
shutil.copy(os.path.join(s, fname), t)
Output

Explanation: os.listdir(s) lists all files and directories in the source directory and stores them in the files variable. The for loop iterates over each file (fname) and os.path.join(s, fname) creates the full path. shutil.copy() then copies the file content to the destination directory without preserving metadata.
Similar Reads
How to move all files from one directory to another using Python ? In this article, we will see how to move all files from one directory to another directory using Python. Â In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil m
2 min read
How to print all files within a directory using Python? The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to
3 min read
List all files of certain type in a directory using Python In python, there are several built-in modules and methods for file handling. These functions are present in different modules such as os, glob, etc. This article helps you find out many of the functions in one place which gives you a brief knowledge about how to list all the files of a certain type
3 min read
Copy all the content of one file to another file in uppercase - Python Copying the content of one file to another while transforming the text to uppercase can be easily done using Pythonâs file handling features. By reading the source file, converting its contents to uppercase and writing to a new file, you can achieve this efficiently. Letâs look at the definitions of
3 min read
Python | Move or Copy Files and Directories Let's say we want to copy or move files and directories around, but donât want to do it by calling out to shell commands. The shutil module has portable implementations of functions for copying files and directories. Code #1 : Using shutil module Python3 1== import shutil # Copy src to dst. (cp src
3 min read
Python - Read file from sibling directory In this article, we will discuss the method to read files from the sibling directory in Python. First, create two folders in a root folder, and one folder will contain the python file and the other will contain the file which is to be read. Below is the dictionary tree: Directory Tree: root : | |__S
3 min read