Standard Library
Python shutil Module
Python File Operations
Python shutil module copies and moves files, with rmtree.
Introduction to the shutil Module
The shutil
module in Python is a powerful utility used for high-level file operations such as copying and moving files and directories. It provides a convenient way to handle file operations without having to manually handle the low-level details.
Copying Files with shutil
The shutil.copy()
function copies the content of a source file to a destination file or directory. If the destination is a directory, the file will be copied into that directory.
The shutil.copy2()
function is similar but also attempts to preserve file metadata.
Moving Files with shutil
To move a file or directory, you can use the shutil.move()
function. This function moves a file or directory to a new location.
Deleting Files and Directories
The shutil.rmtree()
function is used to delete an entire directory tree. Use this with caution as it will permanently delete all contents within the directory.
Conclusion
The shutil
module is a high-level interface for file operations in Python. Whether you need to copy, move, or delete files and directories, shutil
provides a straightforward approach to managing files efficiently.
Standard Library
- datetime Module
- math Module
- random Module
- os Module
- sys Module
- json Module
- re Module
- time Module
- collections Module
- itertools Module
- argparse Module
- logging Module
- urllib Module
- shutil Module
- glob Module
- statistics Module
- calendar Module
- zipfile Module
- pickle Module
- threading Module
- subprocess Module
- Requests Module
- cMath Module
- Previous
- urllib Module
- Next
- glob Module