Mod4_ppt
Mod4_ppt
The shutil (or shell utilities) module has functions to copy, move,
rename, and delete files in python programs.
ZIP file can also package multiple files and subfolders into one
Python programs can create and open (or extract) ZIP files using
functions in the zip file module
Reading ZIP Files
The command at ➊
calculates how efficiently
example.zip is compressed
The string you pass to extract() must match one of the strings
in the list returned by namelist()
The write mode will erase all existing contents of a ZIP file
To simply add files to an existing ZIP file, pass 'a' as the second
argument to zipfile.ZipFile() to open the ZIP file in append
mode
DEBUGGING
RAISING EXCEPTIONS
Python raises an exception whenever it tries to execute
invalid code.
But you can also 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”.
Exceptions are raised with a raise statement.
In code, a raise statement consists of the following:
➢ The raise keyword
➢ A call to the Exception() function
➢ A string with a helpful error message passed to the
Exception() function
For example, enter the following into the
interactive shell:
>>> raise Exception('This is the error message.')
Traceback (most recent call last): File "", line 1, in raise Exception('This is the
error message.')
Exception: This is the error message.
GETTING THE TRACEBACK AS A
STRING
When Python encounters an error, it produces
a treasure trove of error information called the
traceback.
The traceback includes the error message, the
line number of the line that caused error, and
the sequence of the function calls that led to
the error.
This sequence of calls is called the call stack.
Assertions
An assertion is a sanity check to make sure your
code isn’t doing something obviously wrong.
These sanity checks are performed by assert
statements. If the sanity check fails, then an
AssertionError exception is raised.
In code, an assert statement consists of the
following:
➢ The assert keyword A condition (that is, an
expression that evaluates to True or False)
➢ A comma
➢ A string to display when the condition is False
Example