shutil.rmtree()

shutil.rmtree(path, ignore_errors=False, onerror=None) #Recursively delete files

def rmtree(path, ignore_errors=False, onerror=None):
    """Recursively delete a directory tree.

    If ignore_errors is set, errors are ignored; otherwise, if onerror
    is set, it is called to handle the error with arguments (func,
    path, exc_info) where func is platform and implementation dependent;
    path is the argument to that function that caused it to fail; and
    exc_info is a tuple returned by sys.exc_info().  If ignore_errors
    is false and onerror is None, an exception is raised.

    """
    if ignore_errors:
        def onerror(*args):
            pass
    elif onerror is None:
        def onerror(*args):
            raise
    if _use_fd_functions:
        # While the unsafe rmtree works fine on bytes, the fd based does not.
        if isinstance(path, bytes):
            path = os.fsdecode(path)
        # Note: To guard against symlink races, we use the standard
        # lstat()/open()/fstat() trick.
        try:
            orig_st = os.lstat(path)
        except Exception:
            onerror(os.lstat, path, sys.exc_info())
            return
        try:
            fd = os.open(path, os.O_RDONLY)
        except Exception:
            onerror(os.lstat, path, sys.exc_info())
            return
        try:
            if os.path.samestat(orig_st, os.fstat(fd)):
                _rmtree_safe_fd(fd, path, onerror)
                try:
                    os.rmdir(path)
                except OSError:
                    onerror(os.rmdir, path, sys.exc_info())
            else:
                try:
                    # symlinks to directories are forbidden, see bug #1669
                    raise OSError("Cannot call rmtree on a symbolic link")
                except OSError:
                    onerror(os.path.islink, path, sys.exc_info())
        finally:
            os.close(fd)
    else:
        return _rmtree_unsafe(path, onerror)

# Allow introspection of whether or not the hardening against symlink
# attacks is supported on the current platform
rmtree.avoids_symlink_attacks = _use_fd_functions

shutil other modules

shutil.copyfile(src, dst) #Copy from source src to dst. If the current dst already exists, it will be overwritten
shutil.move(src, dst) #Move the file or rename
shutil.copymode(src, dst) #Just copy its permissions Other things are
shutil that will not be copied .copystat(src, dst) #Copy permissions, last access time, last modification time
shutil.copy(src, dst) #Copy a file to a file or a directory
shutil.copy2(src, dst) #Base on copy The last access time and modification time of the file are also copied, something similar to cp -p
shutil.copy2( src, dst) #If the file systems of the two locations are the same, it is equivalent to a rename operation, but the name is changed; if If it is not in the same file system, do the move operation
shutil.copytree( olddir, newdir, True/Fase) #Copy olddir to newdir, if the third parameter is True, the symbols under the folder will be kept when copying the directory connection, if the third parameter is False, a physical copy will be generated in the copied directory to replace the symbolic link

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325890359&siteId=291194637