How to copy directory recursively in python and overwrite all?
Notice:
distutils
has been deprecated and will be removed in Python 3.12. Consider looking for other answers at this question if you are looking for a post-3.12 solution.
Original answer:
You can use distutils.dir_util.copy_tree
. It works just fine and you don't have to pass every argument, only src
and dst
are mandatory.
However in your case you can't use a similar tool likeshutil.copytree
because it behaves differently: as the destination directory must not exist this function can't be used for overwriting its contents.
If you want to use the cp
tool as suggested in the question comments beware that using the subprocess
module is currently the recommended way for spawning new processes as you can see in the documentation of the os.system function.
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
How to recursively copy all files with a certain extension in a directory using Python?
If I understand correctly, you want glob
with recursive=True
, which, with the **
specifier, will recursively traverse directories and find all files satisfying a format specifier:
import glob
import os
import shutil
def copy(src, dest):
for file_path in glob.glob(os.path.join(src, '**', '*.bmp'), recursive=True):
new_path = os.path.join(dest, os.path.basename(file_path))
shutil.copy(file_path, new_path)
Python: copy folder content recursively
I found the answer with the help of tdelaney:
source_folder is the path to the source and destination_folder the path to the destination.
import os
import shutil
def copyrecursively(source_folder, destination_folder):
for root, dirs, files in os.walk(source_folder):
for item in files:
src_path = os.path.join(root, item)
dst_path = os.path.join(destination_folder, src_path.replace(source_folder, ""))
if os.path.exists(dst_path):
if os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
shutil.copy2(src_path, dst_path)
else:
shutil.copy2(src_path, dst_path)
for item in dirs:
src_path = os.path.join(root, item)
dst_path = os.path.join(destination_folder, src_path.replace(source_folder, ""))
if not os.path.exists(dst_path):
os.mkdir(dst_path)
Iteratively (or recursively?) filter directory and copy results to new directory
Now I haven't tested this since I don't have all of the files to test with. However, I would just make the "pattern" variable a list and add another for loop. I also changed the "dest" variable so we can just utilize the "patterns" list.
import os
import shutil
import fnmatch
dir = '/my_dir_path'
patterns = ['aaa', 'bbb', 'ccc']
dest = '/my_new_dir_path/'
for root, dirs, files in os.walk(dir):
for pattern in patterns:
for filename in fnmatch.filter(files, pattern+'_*'):
source = (os.path.join(root, filename))
shutil.copy2(source, dest+pattern+'/')
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
.
copy folder, subfolders and files from a path to another path in python via a recursive function
Use os.path.isdir
instead of os.path.exists
to ensure that it can only be a directory not a file. And os.path.join
is better than concatenating path strings by ourselves.
def CopyFol_Subfolders(src, dst):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
CopyFol_Subfolders(s, d)
else:
shutil.copy2(s, d)
Recursively find and copy files from many folders
I think this should do what you need assuming they are all .txt files.
import glob
import shutil
filenames_i_want = ['A_010720_X.txt','B_120720_Y.txt']
TargetFolder = r'C:\ELK\LOGS\ATH\DEST'
all_files = []
for directory in ['A', 'B']:
files = glob.glob('C:\{}\*\DL\*.txt'.format(directory))
all_files.append(files)
for file in all_files:
if file in filenames_i_want:
shutil.copy2(file, TargetFolder)
Related Topics
Python Raises Syntaxerror on "=" in If Statement
Why Can't I Change Only a Single Element in a Nested List in Python
Python: Urllib2 How to Send Cookie with Urlopen Request
Find Usa Phone Numbers in Python Script
Pyinstaller Is Not Recognized as Internal or External Command
Why Doesn't 2._Add_(3) Work in Python
Unicodeencodeerror: 'Ascii' Codec Can't Encode Character '\Xe9' - -When Using Urlib.Request Python3
Catch Exception and Continue Try Block in Python
Importerror After Cython Embed
Create a Main Loop with Tkinter
How to Print a Percentage Value in Python
Using a Where _ in _ Statement
How to Find Tags with Only Certain Attributes - Beautifulsoup