Monday, January 20, 2025
HomeProgrammingShutil Module in Python

Shutil Module in Python

The shutil module in Python provides a number of high-level operations on files and collections of files. It allows you to copy, move, remove, and manage files and directories in a simple way.

Here are some of the most commonly used functions in the shutil module:

1. Copying Files

  • shutil.copy(src, dst): Copies the file from src to dst. The file’s content and its permissions are copied, but metadata like timestamps are not.
  • shutil.copy2(src, dst): Similar to copy, but it also copies the file’s metadata (timestamps).
  • shutil.copyfile(src, dst): Copies the contents of the file from src to dst, but does not copy metadata or permissions.
  • shutil.copytree(src, dst): Recursively copies an entire directory tree from src to dst.

2. Moving Files

  • shutil.move(src, dst): Moves a file or directory from src to dst. If the destination is a directory, it will move the source into that directory.
See also  How can I reset the identity seed after deleting records in SQL Server?

3. Removing Files or Directories

  • shutil.rmtree(path): Removes an entire directory tree at path, including all its contents.
  • shutil.remove(path): Removes a single file, just like os.remove.

4. Disk Usage and File Operations

  • shutil.disk_usage(path): Returns the disk usage statistics about the given path, including total, used, and free space.

5. Creating and Extracting Archives

  • shutil.make_archive(base_name, format, root_dir, ...): Creates an archive file (like .zip, .tar, .gztar, etc.) from the contents of a directory.
  • shutil.unpack_archive(filename, extract_dir): Extracts the contents of an archive (such as .zip, .tar, etc.) to a specified directory.
See also  What is Bootstrapping?

6. File Permissions and Ownership

  • shutil.chown(path, user=None, group=None): Changes the owner and/or group of the file or directory at path.

Example Usage:

import shutil

# Copying a file
shutil.copy('source.txt', 'destination.txt')

# Copying a directory tree
shutil.copytree('source_dir', 'destination_dir')

# Moving a file
shutil.move('source.txt', 'new_location.txt')

# Removing a directory tree
shutil.rmtree('old_dir')

# Getting disk usage
usage = shutil.disk_usage('/')
print(f'Total: {usage.total}, Used: {usage.used}, Free: {usage.free}')

# Creating an archive
shutil.make_archive('archive_name', 'zip', 'source_dir')

# Extracting an archive
shutil.unpack_archive('archive_name.zip', 'extract_dir')

Notes:

  • shutil is part of the standard library, so you don’t need to install any external packages to use it.
  • Many functions in shutil can raise exceptions like FileNotFoundError or PermissionError, so it’s good practice to handle these cases with try/except blocks where necessary.
See also  How to Get the IP Address in PHP

The shutil module simplifies common file and directory operations, making it a powerful tool for file system management in Python.

RELATED ARTICLES

Banking Application in Java

Java PrintWriter Class

What Is CSS Hover?

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x