---
title: shutil
---
## copymode

```python
def copymode(
    src, dst, *, follow_symlinks:bool=True
):
```

*Copy mode bits from src to dst.*

If follow\_symlinks is not set, symlinks aren't followed if and only
if both `src` and `dst` are symlinks.  If `lchmod` isn't available
(e.g. Linux) this method does nothing.

## copystat

```python
def copystat(
    src, dst, *, follow_symlinks:bool=True
):
```

*Copy file metadata*

Copy the permission bits, last access time, last modification time, and
flags from `src` to `dst`. On Linux, copystat() also copies the "extended
attributes" where possible. The file contents, owner, and group are
unaffected. `src` and `dst` are path-like objects or path names given as
strings.

If the optional flag `follow_symlinks` is not set, symlinks aren't
followed if and only if both `src` and `dst` are symlinks.

## copy

```python
def copy(
    src, dst, *, follow_symlinks:bool=True
):
```

*Copy data and mode bits ("cp src dst"). Return the file's destination.*

The destination may be a directory.

If follow\_symlinks is false, symlinks won't be followed. This
resembles GNU's "cp -P src dst".

If source and destination are the same file, a SameFileError will be
raised.

## copy2

```python
def copy2(
    src, dst, *, follow_symlinks:bool=True
):
```

*Copy data and metadata. Return the file's destination.*

Metadata is copied with copystat(). Please see the copystat function
for more information.

The destination may be a directory.

If follow\_symlinks is false, symlinks won't be followed. This
resembles GNU's "cp -P src dst".

## move

```python
def move(
    src, dst, copy_function:function=copy2
):
```

*Recursively move a file or directory to another location. This is similar to the Unix "mv" command. Return the file or directory's destination.*

If dst is an existing directory or a symlink to a directory, then src is
moved inside that directory. The destination path in that directory must
not already exist.

If dst already exists but is not a directory, it may be overwritten
depending on os.rename() semantics.

os.rename() is preferably used if the source and destination are on the
same filesystem. In case os.rename() fails due to OSError (e.g. the user
has write permission to *dst* file but not to its parent directory),
this method falls back to using *copy\_function* silently.
Symlinks are also recreated under the new name if os.rename() fails
because of cross filesystem renames.

It's recommended to use os.rename() if atomic move is strictly required.

The optional `copy_function` argument is a callable that will be used
to copy the source or it will be delegated to [`copytree`](shutil.md#copytree).
By default, copy2() is used, but any function that supports the same
signature (like copy()) can be used.

A lot more could be done here...  A look at a mv.c shows a lot of
the issues this implementation glosses over.

## copytree

```python
def copytree(
    src, dst, symlinks:bool=False, ignore=None, copy_function:function=copy2, ignore_dangling_symlinks:bool=False,
    dirs_exist_ok:bool=False
):
```

*Recursively copy a directory tree and return the destination directory.*

If exception(s) occur, an Error is raised with a list of reasons.

If the optional symlinks flag is true, symbolic links in the
source tree result in symbolic links in the destination tree; if
it is false, the contents of the files pointed to by symbolic
links are copied. If the file pointed to by the symlink doesn't
exist, an exception will be added in the list of errors raised in
an Error exception at the end of the copy process.

You can set the optional ignore\_dangling\_symlinks flag to true if you
want to silence this exception. Notice that this has no effect on
platforms that don't support os.symlink.

The optional ignore argument is a callable. If given, it
is called with the `src` parameter, which is the directory
being visited by copytree(), and `names` which is the list of
`src` contents, as returned by os.listdir():

```
callable(src, names) -> ignored_names
```

Since copytree() is called recursively, the callable will be
called once for each directory that is copied. It returns a
list of names relative to the `src` directory that should
not be copied.

The optional copy\_function argument is a callable that will be used
to copy each file. It will be called with the source path and the
destination path as arguments. By default, copy2() is used, but any
function that supports the same signature (like copy()) can be used.

If dirs\_exist\_ok is false (the default) and `dst` already exists, a
`FileExistsError` is raised. If `dirs_exist_ok` is true, the copying
operation will continue if it encounters existing directories, and files
within the `dst` tree will be overwritten by corresponding files from the
`src` tree.

## rmtree

```python
def rmtree(
    path, ignore_errors:bool=False, onerror=None, *, onexc=None, dir_fd=None, _onexc_kwargs:bool=False
):
```

*Recursively delete a directory tree.*

If dir\_fd is not None, it should be a file descriptor open to a directory;
path will then be relative to that directory.
dir\_fd may not be implemented on your platform.
If it is unavailable, using it will raise a NotImplementedError.

If ignore\_errors is set, errors are ignored; otherwise, if onexc or
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
the value of exc\_info describes the exception. For onexc it is the
exception instance, and for onerror it is a tuple as returned by
sys.exc\_info().  If ignore\_errors is false and both onexc and
onerror are None, the exception is reraised.

onerror is deprecated and only remains for backwards compatibility.
If both onerror and onexc are set, onerror is ignored and onexc is used.

## disk\_usage

```python
def disk_usage(
    path
):
```

*Return disk usage statistics about the given path.*

Returned value is a named tuple with attributes 'total', 'used' and
'free', which are the amount of total, used and free space, in bytes.

## chown

```python
def chown(
    path, user=None, group=None, *, dir_fd=None, follow_symlinks:bool=True
):
```

*Change owner user and group of the given path.*

user and group can be the uid/gid or the user/group names, and in that case,
they are converted to their respective uid/gid.

If dir\_fd is set, it should be an open file descriptor to the directory to
be used as the root of *path* if it is relative.

If follow\_symlinks is set to False and the last element of the path is a
symbolic link, chown will modify the link itself and not the file being
referenced by the link.

## rmtree

```python
def rmtree(
    path, ignore_errors:bool=False, onerror=None, *, onexc=None, dir_fd=None, _onexc_kwargs:bool=False
):
```

*Recursively delete a directory tree.*

If dir\_fd is not None, it should be a file descriptor open to a directory;
path will then be relative to that directory.
dir\_fd may not be implemented on your platform.
If it is unavailable, using it will raise a NotImplementedError.

If ignore\_errors is set, errors are ignored; otherwise, if onexc or
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
the value of exc\_info describes the exception. For onexc it is the
exception instance, and for onerror it is a tuple as returned by
sys.exc\_info().  If ignore\_errors is false and both onexc and
onerror are None, the exception is reraised.

onerror is deprecated and only remains for backwards compatibility.
If both onerror and onexc are set, onerror is ignored and onexc is used.
