Python - Standard Libraries

13.
How can you work with compressed files using the gzip and zipfile modules?

The gzip and zipfile modules in Python provide functionalities for working with compressed files. The gzip module is specifically designed for gzip compression, while the zipfile module supports the ZIP file format. These modules are useful for reading and writing compressed files in various applications.

Let's explore examples using both modules to compress and decompress files:

import gzip
import zipfile

# Example using gzip module
input_text = "This is a sample text to be compressed using gzip module."

# Compress the text and write it to a gzip file
with gzip.open('compressed_text.gz', 'wt') as compressed_file:
    compressed_file.write(input_text)

# Decompress the gzip file and read the content
with gzip.open('compressed_text.gz', 'rt') as decompressed_file:
    decompressed_text = decompressed_file.read()
    print("Decompressed Text (gzip):", decompressed_text)

# Example using zipfile module
file_contents = b"This is a sample text to be compressed and stored in a ZIP file."

# Create a ZIP file and add a compressed file to it
with zipfile.ZipFile('compressed_file.zip', 'w') as zip_file:
    zip_file.writestr('compressed_text.txt', file_contents)

# Extract the compressed file from the ZIP file
with zipfile.ZipFile('compressed_file.zip', 'r') as zip_file:
    extracted_contents = zip_file.read('compressed_text.txt')
    print("Extracted Contents (zipfile):", extracted_contents.decode('utf-8'))

Output:

Decompressed Text (gzip): This is a sample text to be compressed using gzip module.
Extracted Contents (zipfile): This is a sample text to be compressed and stored in a ZIP file.

In the first example, the gzip module is used to compress and decompress a text file. In the second example, the zipfile module is used to create a ZIP file containing a compressed file, and then extract the compressed content from the ZIP file.

These modules are valuable for scenarios where file size matters, and compression is necessary for storage or transmission. They provide a convenient way to work with compressed data in Python.


14.
Explain the significance of the csv module for working with CSV files.

The csv module in Python is a built-in module that provides functionalities for reading from and writing to CSV (Comma-Separated Values) files. CSV is a common format used for tabular data representation, and the csv module simplifies the process of handling such files in Python.

Let's explore an example using the csv module to read from and write to a CSV file:

import csv

# Example data for writing to a CSV file
data_to_write = [
    ['Name', 'Age', 'City'],
    ['John', 25, 'New York'],
    ['Alice', 30, 'London'],
    ['Bob', 22, 'Paris']
]

# Write data to a CSV file
with open('example.csv', 'w', newline='') as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerows(data_to_write)

# Read data from the CSV file
data_read = []
with open('example.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)
    for row in csv_reader:
        data_read.append(row)

# Display the read data
for row in data_read:
    print(row)

Output:

['Name', 'Age', 'City']
['John', '25', 'New York']
['Alice', '30', 'London']
['Bob', '22', 'Paris']

In this example, we use the csv.writer() to write data to a CSV file and csv.reader() to read data from a CSV file. The newline='' argument in the open() function is used to ensure consistent newline handling across different platforms.

The csv module supports various customization options, including specifying the delimiter, quoting characters, and handling headers. It is a convenient tool for handling CSV files in Python, making it easy to integrate CSV data into your Python applications.


15.
How do you use the pickle module for object serialization in Python?

The pickle module in Python is used for object serialization, allowing you to convert complex data structures, such as Python objects, into a format that can be easily stored or transmitted. Pickling refers to the process of converting a Python object into a byte stream, and unpickling refers to the process of reconstructing the original object from the byte stream.

Let's explore an example using the pickle module to serialize and deserialize a Python object:

import pickle

# Example data to be pickled
data_to_pickle = {'name': 'John', 'age': 25, 'city': 'New York'}

# Pickle the data and write it to a file
with open('pickled_data.pkl', 'wb') as pickle_file:
    pickle.dump(data_to_pickle, pickle_file)

# Unpickle the data from the file
with open('pickled_data.pkl', 'rb') as pickle_file:
    unpickled_data = pickle.load(pickle_file)

# Display the unpickled data
print("Unpickled Data:", unpickled_data)

Output:

Unpickled Data: {'name': 'John', 'age': 25, 'city': 'New York'}

In this example, we use pickle.dump() to serialize and write the data to a file, and pickle.load() to read and deserialize the data from the file. The file is opened in binary mode ('wb' and 'rb') to ensure proper handling of binary data.

The pickle module is useful for scenarios where you need to store or transmit complex Python objects, such as dictionaries, lists, or custom objects. However, caution should be exercised when unpickling data from untrusted sources, as it may pose security risks.


16.
Discuss the role of the subprocess module for running external processes.

The subprocess module in Python provides a way to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. It allows you to interact with external programs from within your Python script, making it versatile for tasks such as running shell commands, calling executables, and more.

Let's explore an example using the subprocess module to run a simple shell command:

import subprocess

# Run a shell command to list files in the current directory
command = 'ls'
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

# Display the command output and return code
print("Command Output:")
print(result.stdout)
print("Return Code:", result.returncode)

Output:

Command Output:
file1.txt
file2.txt
file3.txt
Return Code: 0

In this example, we use subprocess.run() to run the shell command 'ls' (list files) with the shell=True argument. The stdout=subprocess.PIPE and stderr=subprocess.PIPE arguments capture the command's standard output and standard error, respectively. The text=True argument ensures that the output is returned as text.

The subprocess module provides various functions for different use cases, such as subprocess.call(), subprocess.check_output(), and subprocess.Popen(). It is a powerful tool for integrating external processes into Python scripts, enabling automation and integration with system-level commands.