I'm trying to copy /home/myUser/dir1/
and all its contents (and their contents, etc.) to /home/myuser/dir2/
in python. Furthermore, I want the copy to overwrite everything in dir2/
.
It looks like distutils.dir_util.copy_tree
might be the right tool for the job, but not sure if there's anything easier/more obvious to use for such a simple task.
If it is the right tool, how do I use it? According to the docs there are 8 parameters that it takes. Do I have to pass all 8 are just src
, dst
and update
, and if so, how (I'm brand new to Python).
If there's something out there that's better, can someone give me an example and point me in the right direction? Thanks in advance!
shutil. copytree() method recursively copies an entire directory tree rooted at source (src) to the destination directory. The destination directory, named by (dst) must not already exist. It will be created during copying.
To overwrite a file, to write new content into a file, we have to open our file in “w” mode, which is the write mode. It will delete the existing content from a file first; then, we can write new content and save it. We have a new file with the name “myFile. txt”.
The copy2() method in Python is used to copy the content of the source file to the destination file or directory. This method is identical to shutil. copy() method also preserving the file's metadata.
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.
Have a look at the shutil
package, especially rmtree
and copytree
. You can check if a file / path exists with os.paths.exists(<path>)
.
import shutil import os def copy_and_overwrite(from_path, to_path): if os.path.exists(to_path): shutil.rmtree(to_path) shutil.copytree(from_path, to_path)
Vincent was right about copytree
not working, if dirs already exist. So distutils
is the nicer version. Below is a fixed version of shutil.copytree
. It's basically copied 1-1, except the first os.makedirs()
put behind an if-else-construct:
import os from shutil import * def copytree(src, dst, symlinks=False, ignore=None): names = os.listdir(src) if ignore is not None: ignored_names = ignore(src, names) else: ignored_names = set() if not os.path.isdir(dst): # This one line does the trick os.makedirs(dst) errors = [] for name in names: if name in ignored_names: continue srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if symlinks and os.path.islink(srcname): linkto = os.readlink(srcname) os.symlink(linkto, dstname) elif os.path.isdir(srcname): copytree(srcname, dstname, symlinks, ignore) else: # Will raise a SpecialFileError for unsupported file types copy2(srcname, dstname) # catch the Error from the recursive copytree so that we can # continue with other files except Error, err: errors.extend(err.args[0]) except EnvironmentError, why: errors.append((srcname, dstname, str(why))) try: copystat(src, dst) except OSError, why: if WindowsError is not None and isinstance(why, WindowsError): # Copying file access times may fail on Windows pass else: errors.extend((src, dst, str(why))) if errors: raise Error, errors
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With