110. Python's shutil Module
The shutil module in Python provides a high-level interface for file operations such as copying, moving, and archiving files and directories. Here are 10 Python code snippets to demonstrate various file operations using shutil:
1. Copying Files
You can use shutil.copy() to copy a file from one location to another.
import shutil
# Copy a file from 'source.txt' to 'destination.txt'
shutil.copy('source.txt', 'destination.txt')
print("File copied successfully.")Explanation:
shutil.copy()copies the contents and permissions of the source file to the destination.
2. Copying Files with Metadata
To copy both the file content and metadata (such as permissions, times), use shutil.copy2().
import shutil
# Copy a file with metadata from 'source.txt' to 'destination.txt'
shutil.copy2('source.txt', 'destination.txt')
print("File copied with metadata.")Explanation:
shutil.copy2()copies both the contents and the metadata of the source file.
3. Copying a Directory
You can copy an entire directory and its contents with shutil.copytree().
Explanation:
shutil.copytree()recursively copies all files and subdirectories from the source to the destination.
4. Moving Files or Directories
To move a file or directory from one location to another, use shutil.move().
Explanation:
shutil.move()moves a file or directory to a new location.
5. Renaming Files or Directories
Renaming a file or directory can be done using shutil.move(), which can also handle renaming.
Explanation:
shutil.move()can also rename files by moving them within the same directory.
6. Deleting a Directory Tree
You can delete an entire directory tree using shutil.rmtree().
Explanation:
shutil.rmtree()removes a directory and all its contents (files and subdirectories).
7. Creating an Archive (ZIP or TAR)
To create a compressed archive of a file or directory, use shutil.make_archive().
Explanation:
shutil.make_archive()creates a compressed archive (in this case, a.zipfile) of a directory or file.
8. Extracting an Archive (ZIP or TAR)
You can extract the contents of an archive file using shutil.unpack_archive().
Explanation:
shutil.unpack_archive()extracts an archive file (zip, tar, etc.) to the specified destination directory.
9. Disk Usage Information
To get disk usage statistics, such as total, used, and free space, use shutil.disk_usage().
Explanation:
shutil.disk_usage()returns the total, used, and free disk space of the specified path.
10. File Permissions
You can change the permissions of a file or directory using shutil.chmod().
Explanation:
shutil.chmod()changes the file permissions.The permissions are set using constants from the
statmodule.
Key Takeaways:
shutil.copy(): Copies a file, excluding metadata.shutil.copy2(): Copies a file, including metadata.shutil.copytree(): Copies an entire directory tree.shutil.move(): Moves or renames files or directories.shutil.rmtree(): Removes a directory tree.shutil.make_archive(): Creates compressed archives like ZIP or TAR.shutil.unpack_archive(): Extracts archives.shutil.disk_usage(): Provides disk usage statistics.shutil.chmod(): Changes file permissions.
These file operations are powerful tools in Python for managing files and directories, whether you're copying, moving, or compressing them.
Last updated