Notes Module4 Python Programming
Notes Module4 Python Programming
Module 4 Syllabus:
Organizing Files: The shutil Module, Walking a Directory Tree, Compressing Files with the
zipfile Module, Project: Renaming Files with American-Style Dates to European-Style
Dates,Project: Backing Up a Folder into a ZIP File
Debugging: Raising Exceptions, Getting the Traceback as a String, Assertions, Logging, IDLE‟s
Debugger.
shultil.copy()
shutil.copytree()
os.walk()
Compressing files with the zipfile module
Chapter 10
Debugging
Raising Exceptions
● We can raise your own exceptions in your code. Raising an exception is a way of saying,
“Stop running the code in this function and move the program execution to the except
statement.”
● If there are no try and except statements covering the raise statement that raised
the exception, the program simply crashes and displays the exception’s error message.
● Often it’s the code that calls the function, not the function itself, that knows how to
handle an exception.
● So you will commonly see a raise statement inside a function and the try and
except statements in the code calling the function.
OUTPUT
Assertions
- Using an Assertion in a Traffic Light Simulation
Say you’re building a traffic light simulation program. The data structure representing the
stoplights at an intersection is a dictionary with keys 'ns' and 'ew', for the stoplights facing
north-south and east-west, respectively. The values at these keys will be one of the strings
'green', ' yellow', or 'red'. The code would look something like this:
Logging
● If you’ve ever put a print() statement in your code to output some variable’s value while
your program is running, you’ve used a form of logging to debug your code.
● Logging is a great way to understand what’s happening in your program and in what
order it's happening.
● Python’s logging module makes it easy to create a record of custom messages that you
write. These log messages will describe when the program execution has reached the
logging function call and list any variables you have specified at that point in time.
● On the other hand, a missing log message indicates a part of the code was skipped and
never executed.
To enable the logging module to display log messages on your screen as your program runs,
copy the following to the top of your program.
● Here, we use the logging.debug() function when we want to print log information. This
debug() function will call basicConfig(), and a line of information will be printed.
● This information will be in the format we specified in basicConfig() and will include the
messages we passed to debug().
● The print(factorial(5)) call is part of the original program, so the result is displayed even
if logging messages are disabled.
IDLE’s Debugger
● The debugger is a feature of IDLE that allows you to execute your program one line at a
time. The debugger will run a single line of code and then wait for you to tell it to
continue.
● By running your program “under the debugger” like this, you can take as much time as
you want to examine the values in the variables at any given point during the program’s
lifetime. This is a valuable tool for tracking down bugs.
● When the Debug Control window appears, select all four of the Stack, Locals, Source,
and Globals checkboxes so that the window shows the full set of debug information.
● While the Debug Control window is displayed, any time you run a program from the file
editor, the debugger will pause execution before the first instruction and display the
following:
1. The line of code that is about to be executed
2. A list of all local variables and their values
3. A list of all global variables and their value
● The program will stay paused until you press one of the five buttons in the Debug
Control window:
1) Go
2) Step
3) Over
4) Out
5) Quit