Python shutil Module
Example
Copy a file and get disk usage:
import shutil
usage = shutil.disk_usage('.')
print(f'Total space: {usage.total // (1024**3)} GB')
print(f'Free space: {usage.free // (1024**3)} GB')
Try it Yourself »
Definition and Usage
The shutil module provides high-level operations on files and collections of files.
Use it to copy, move, or remove files and directories, create archives, or query disk usage information.
Members
Member | Description |
---|---|
Error | Base exception class for shutil errors. |
SameFileError | Exception raised when source and destination are the same file. |
SpecialFileError | Exception raised for special files (pipes, devices). |
chown() | Change owner and/or group of a file. |
copy() | Copy a file to a destination file or directory. |
copy2() | Copy file with metadata (timestamps, permissions). |
copytree() | Recursively copy an entire directory tree. |
disk_usage() | Return disk usage statistics (total, used, free). |
get_archive_formats() | Return list of supported archive formats. |
get_terminal_size() | Return the size of the terminal window. |
make_archive() | Create an archive file (tar, zip, etc.). |
move() | Recursively move a file or directory to another location. |
rmtree() | Recursively delete a directory tree. |
unpack_archive() | Extract an archive file. |
which() | Return the path to an executable program. |