0% found this document useful (0 votes)
1 views4 pages

Python Exam Module2

The document provides exam preparation questions and answers related to Python modules such as File Handling, Assertions, Debugging, and Logging. It explains how to copy and delete files using the shutil and send2trash modules, the use of assertions for debugging, logging levels, and directory walking with os.walk(). Additionally, it covers backing up folders into zip files using shutil, with examples for each topic to aid in understanding and exam readiness.

Uploaded by

madivalshridhar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Python Exam Module2

The document provides exam preparation questions and answers related to Python modules such as File Handling, Assertions, Debugging, and Logging. It explains how to copy and delete files using the shutil and send2trash modules, the use of assertions for debugging, logging levels, and directory walking with os.walk(). Additionally, it covers backing up folders into zip files using shutil, with examples for each topic to aid in understanding and exam readiness.

Uploaded by

madivalshridhar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Exam Preparation: Additional Questions from Other Modules

Module: File Handling, Shutil, Shelve, send2trash, and Logging

1. Explain how to copy files and folders using the `` module in Python. Provide an example.

The shutil module in Python provides utility functions for file and directory operations like copying,
moving, or removing. It stands for "shell utilities" and is used heavily in file management tasks.

Important Methods:

• shutil.copy(src, dst) – Copies a file from src to dst .


• shutil.copy2(src, dst) – Same as copy() but also preserves metadata.
• shutil.copytree(src, dst) – Recursively copies an entire directory tree.

Example:

import shutil

# Copy a single file


shutil.copy('source.txt', 'backup.txt')

# Copy a folder recursively


shutil.copytree('my_folder', 'my_folder_backup')

These methods are useful for creating backups, automating organization, or transferring data.

2. How can we delete files and folders using the `` module? Why is it preferred over os.remove()?

send2trash sends files or folders to the recycle bin or trash rather than permanently deleting them. It's
safer compared to os.remove() or shutil.rmtree() because you can recover files if needed.

Installation:

pip install send2trash

Example:

from send2trash import send2trash

1
# Safer deletion
send2trash('old_notes.txt')

Why prefer ``?

• Prevents accidental permanent deletion


• Allows users to recover important files
• Essential in apps where user data safety is important

Module: Assertions, Debugging, and Logging

3. What is an assertion in Python? Write an example and explain the output.

An assert statement tests if a condition is True . If not, it raises an AssertionError . It is mostly


used for debugging and validating assumptions in the code.

Syntax:

assert condition, "Error message if condition is False"

Example:

x = 10
assert x > 5, "x is too small"
print("Assertion passed")

Output:

Assertion passed

If x were less than 5, Python would raise:

AssertionError: x is too small

Assertions help you catch bugs early in the development phase.

4. Explain logging levels in Python with an example.

Logging is used to track events during program execution. Python provides different severity levels.

2
Logging Levels:

• DEBUG : Detailed info (for developers)


• INFO : Confirmation things are working
• WARNING : Something unexpected, but program continues
• ERROR : A serious problem
• CRITICAL : Very serious error; program may stop

Example:

import logging

logging.basicConfig(level=logging.DEBUG)

logging.debug("Debug message")
logging.info("Info message")
logging.warning("Warning message")
logging.error("Error message")
logging.critical("Critical message")

Output:

DEBUG:root:Debug message
INFO:root:Info message
WARNING:root:Warning message
ERROR:root:Error message
CRITICAL:root:Critical message

Module: Directory Walking and Backup

5. Explain the concept of walking a directory tree using ``. Give an example.

os.walk() generates the file names in a directory tree by walking top-down or bottom-up. It returns
three values:

1. Directory path ( dirpath )


2. List of directories ( dirnames )
3. List of files ( filenames )

Example:

import os

3
for dirpath, dirnames, filenames in os.walk("my_folder"):
print(f"In {dirpath}:")
for file in filenames:
print(f" - {file}")

Use cases:

• Searching files
• Applying batch operations
• Logging directory structures

6. With suitable code, explain backing up a folder into a zip file using Python.

You can back up a folder using shutil.make_archive() or zipfile module.

Using shutil:

import shutil

# Backup the entire folder


shutil.make_archive("backup_folder", "zip", "my_folder")

This will create a file backup_folder.zip containing all contents of my_folder .

More modules coming next with the same high detail and exam-readiness. Just let me know which module
to prioritize next: OOP, Functions, Dictionaries, or Data Types.

Let’s ace this thing! ✨

You might also like