Move and replace if same file name already exists?
If you specify the full path to the destination (not just the directory) then shutil.move will overwrite any existing file: shutil.move(os.path.join(src, filename), os.path.join(dst, filename))
If you specify the full path to the destination (not just the directory) then shutil.move will overwrite any existing file: shutil.move(os.path.join(src, filename), os.path.join(dst, filename))
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
import glob import shutil dest_dir = “C:/test” for file in glob.glob(r’C:/*.txt’): print(file) shutil.copy(file, dest_dir)
Check this question out: What user do python scripts run as in windows? Apparently the answer is to change the file/folder to not be read-only and then remove it. Here’s onerror() handler from pathutils.py mentioned by @Sridhar Ratnakumar in comments: def onerror(func, path, exc_info): “”” Error handler for “shutil.rmtree“. If the error is due to … Read more
Here’s a solution that’s part of the standard library: from distutils.dir_util import copy_tree copy_tree(“/a/b/c”, “/x/y/z”) See this similar question. Copy directory contents into a directory with python Reference – https://docs.python.org/3/distutils/apiref.html#distutils.dir_util.copy_tree