# 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.

```python
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()`.

```python
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()`.

```python
import shutil

# Copy the contents of 'source_directory' to 'destination_directory'
shutil.copytree('source_directory', 'destination_directory')
print("Directory copied successfully.")
```

**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()`.

```python
import shutil

# Move a file from 'source.txt' to 'destination.txt'
shutil.move('source.txt', 'destination.txt')
print("File moved successfully.")
```

**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.

```python
import shutil

# Rename a file from 'old_name.txt' to 'new_name.txt'
shutil.move('old_name.txt', 'new_name.txt')
print("File renamed successfully.")
```

**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()`.

```python
import shutil

# Delete the directory 'target_directory' and all its contents
shutil.rmtree('target_directory')
print("Directory tree deleted.")
```

**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()`.

```python
import shutil

# Create a zip archive of the 'source_directory'
shutil.make_archive('archive_name', 'zip', 'source_directory')
print("Archive created successfully.")
```

**Explanation**:

* `shutil.make_archive()` creates a compressed archive (in this case, a `.zip` file) 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()`.

```python
import shutil

# Extract the contents of 'archive_name.zip' to 'destination_directory'
shutil.unpack_archive('archive_name.zip', 'destination_directory')
print("Archive extracted successfully.")
```

**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()`.

```python
import shutil

# Get disk usage information for the current directory
total, used, free = shutil.disk_usage('/')

print(f"Total: {total / (2**30):.2f} GB")
print(f"Used: {used / (2**30):.2f} GB")
print(f"Free: {free / (2**30):.2f} GB")
```

**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()`.

```python
import shutil
import stat

# Change the permissions of 'example.txt' to read-write for owner, read-only for others
shutil.chmod('example.txt', stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
print("File permissions changed.")
```

**Explanation**:

* `shutil.chmod()` changes the file permissions.
* The permissions are set using constants from the `stat` module.

***

**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.
