This question already has answers here:
How do I copy an entire directory of files into an existing directory , This limitation of the standard shutil.copytree seems arbitrary and annoying. Workaround: import os, shutil def copytree(src, dst, symlinks=False, ignore=None): Recursively copy an entire directory tree rooted at src, returning the destination directory. The destination directory, named by dst, must not already exist; it will be created as well as missing parent directories. Permissions and times of directories are copied with copystat(), individual files are copied using shutil.copy2().
from subprocess import call
def cp_dir(source, target):
call(['cp', '-a', source, target]) # Linux
It works for me. Basically, it executes shell command
cp. How to Recursively Copy a Folder (Directory) in Python, A guide on recursively copying all files in a Folder (Directory) with Python. We also show you how to ignore certain files or directories, when 1- Copy from a tmp/directory some not all folders, listed in a .txt file. 2- Before copying to the destination location, the destination location will be empty. 3- Once I copy the folders, the destination location should only have the folders as specified in the .txt file.
You can also use glob2 to recursively collect all paths (using ** subfolders wildcard) and then use shutil.copyfile, saving the paths
https://code.activestate.com/pypm/glob2/ Chapter 9 – Organizing Files, at the path destination . The source and destination parameters are both strings. This is an easy to follow Python Get Files In Directory tutorial. Here you will learn how to do directory listing, listing files, listing subdirectories in python. OS and pathlib are python built-in modules that are used to get files in directory.
The python libs are obsolete with this function. I've done one that works correctly:
def copydirectorykut(src, dst):
fitx= open(nom, 'w')
for item in list:
fitx.write("%s\n" % item)
f = open(nom,'r')
for line in f.readlines():
if "." in line:
if not os.path.exists(dst+'/'+line[:-1]):
Python, How do I copy all files in a directory in Python? Copy file or directories recursively in Python. Python seems to have functions for copying files (e.g. shutil.copy) and functions for copying directories (e.g. shutil.copytree) but I haven't found any function that handles both. Sure, it's trivial to check whether you want to copy a file or a directory, but it seems like a strange omission. Python, How do I move a folder to another directory in Python? Copy directory contents into a directory with python [duplicate] of files into an existing directory using Python? I copy an entire directory of files into an Python, How do you copy and paste a file in Python? What we want to do now is to copy the directory Original with all its contents to a new directory, and call that new directory Original-Copy. This can be simply done using the copytree() function, as follows (assuming that everything is happening on the desktop): import shutil shutil.copytree('Original', 'Original-Copy')
10.10. shutil — High-level file operations, 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 Copy the contents of the file-like object fsrc to the file-like object fdst. The integer length, if given, is the buffer size. In particular, a negative length value means to copy the data without looping over the source data in chunks; by default the data is read in chunks to avoid uncontrolled memory consumption.
Comments Are you trying to move or copy the directory over? Your title says move, but your content says copy. Since these are two different things, it matters exactly which one you mean. Maybe give an example of before and after, to make it clearer what you want the effect to be, as well? Thanks for your comment, I'll update the heading. It's copy, not move. Np. Also, like @Xymostech said, we're slightly unclear on the desired output. Do you want: /x/y/z/a/b/c or /x/y/z/c? Your use of
copytree implies the former, but I just want to make sure.
Could you simply delete any
/x/y/z/ directory first (
shutil.rmtree()) and then do
An excellent solution, I suggest adding
update=1 to copy_tree() if that is what you need.
Just bear in mind that copy_tree will fail if you call it twice for same set of args, and you've wiped the destination in the meantime. This is due to path caching in mkpath, see bugs.python.org/issue10948 What if I want to overwrite all existing files no matter what. Does this function overwrite existing files if update=0? funnily enough shutil.copy2 in a loop is much faster than copying the directory as a whole. FYI pathlib Path objects break dir_utils, which wants paths as text. yes, but this is not pythonic way :) @ArtsiomPraneuski I agree, but sometimes may be helpful. :) Does not work aside POSIX this is called ''Kludge"