How do I copy an entire directory of files into an existing directory using Python?
This limitation of the standard shutil.copytree
seems arbitrary and annoying. Workaround:
import os, shutil
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
Note that it's not entirely consistent with the standard copytree
:
- it doesn't honor
symlinks
andignore
parameters for the root directory of thesrc
tree; - it doesn't raise
shutil.Error
for errors at the root level ofsrc
; - in case of errors during copying of a subtree, it will raise
shutil.Error
for that subtree instead of trying to copy other subtrees and raising single combinedshutil.Error
.
Python. Copy Files and Folders to existing directory
You can use the copytree function from the shutil package https://docs.python.org/3/library/shutil.html#shutil.copytree.
from shutil import copytree
src_path = "path/of/your/source/dir"
dest_path = "path/of/your/destination/dir"
copytree(src_path, dest_path, dirs_exist_ok=True)
The dirs_exist_ok=True
parameter is useful if the dest_path already exists, and you want to overwrite the existing files.
How to copy a folder into an existing folder using python
You can use the copytree()
method. Because it copy the actual content, you need to create the folder first with os.mkdir
import shutil
import os
directory = "foo"
path = os.path.join(parent_dir, directory)
os.mkdir(path)
source_dir = r"C:\foo"
destination_dir = r"C:\bar\foo"
shutil.copytree(source_dir, destination_dir)
Some documentation
Another solution is to use directly the command line and launching the copy/paste command from there
You can do it by importing the os import os
and then with os.system(my_command)
where my_command
is the string containing the actual command. In linux you can use cp -r directory-1 directory-2
(to copy a directory, you need to add the -r
(or -R
) flag—which is shorthand for --recursive
)
os.system('cp -r C:\foo C:\bar')
Copy file or directories recursively in Python
I suggest you first call shutil.copytree
, and if an exception is thrown, then retry with shutil.copy
.
import shutil, errno
def copyanything(src, dst):
try:
shutil.copytree(src, dst)
except OSError as exc: # python >2.5
if exc.errno in (errno.ENOTDIR, errno.EINVAL):
shutil.copy(src, dst)
else: raise
Copy directory contents into a directory with python
I found this code working which is part of the standard library:
from distutils.dir_util import copy_tree
# copy subdirectory example
from_directory = "/a/b/c"
to_directory = "/x/y/z"
copy_tree(from_directory, to_directory)
Reference:
- Python 2: https://docs.python.org/2/distutils/apiref.html#distutils.dir_util.copy_tree
- Python 3: https://docs.python.org/3/distutils/apiref.html#distutils.dir_util.copy_tree
How to copy files?
shutil
has many methods you can use. One of which is:
import shutil
shutil.copyfile(src, dst)
# 2nd option
shutil.copy(src, dst) # dst can be a folder; use shutil.copy2() to preserve timestamp
- Copy the contents of the file named
src
to a file nameddst
. Bothsrc
anddst
need to be the entire filename of the files, including path. - The destination location must be writable; otherwise, an
IOError
exception will be raised. - If
dst
already exists, it will be replaced. - Special files such as character or block devices and pipes cannot be copied with this function.
- With
copy
,src
anddst
are path names given asstr
s.
Another shutil
method to look at is shutil.copy2()
. It's similar but preserves more metadata (e.g. time stamps).
If you use os.path
operations, use copy
rather than copyfile
. copyfile
will only accept strings.
Moving all files from one directory to another using Python
Try this:
import shutil
import os
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
file_names = os.listdir(source_dir)
for file_name in file_names:
shutil.move(os.path.join(source_dir, file_name), target_dir)
Related Topics
Read Excel Cell Value and Not the Formula Computing It -Openpyxl
Tkinter Gui Layout Using Frames and Grid
Making All Possible Combinations of a List
How to Use an Image for the Background in Tkinter
Pandas Groupby Multiple Fields Then Diff
Using Backslash in Python (Not to Escape)
How to Check Mousebuttonpress Event in Pyqt6
How to Scroll the Background Surface in Pygame
What Is the Easiest Way to Remove All Packages Installed by Pip
Converting a String to a List of Words
Unicodedecodeerror: 'Ascii' Codec Can't Decode Byte 0Xef in Position 1