0% found this document useful (0 votes)
4 views

Mod4_ppt

The document provides an overview of the shutil module in Python, detailing functions for copying, moving, renaming, and deleting files and folders. It also covers the zipfile module for compressing and extracting files, as well as debugging techniques such as raising exceptions, using assertions, and logging. Additionally, it describes the functionality of IDLE's debugger, including breakpoints and various debugging controls.

Uploaded by

h13584962
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Mod4_ppt

The document provides an overview of the shutil module in Python, detailing functions for copying, moving, renaming, and deleting files and folders. It also covers the zipfile module for compressing and extracting files, as well as debugging techniques such as raising exceptions, using assertions, and logging. Additionally, it describes the functionality of IDLE's debugger, including breakpoints and various debugging controls.

Uploaded by

h13584962
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Organizing Files

The shutil Module


The shutil Module

 The shutil (or shell utilities) module has functions to copy, move,
rename, and delete files in python programs.

 To use the shutil functions, the shutil module must be


imported
Copying Files and Folders
 Calling shutil.copy(source, destination) will copy the file at the
path source to the folder at the path destination

 If destination is a filename, it will be used as the new name of


the copied file.
Copying Files and Folders
 shutil.copytree() will copy an entire folder and every folder
and file contained in it

 Calling shutil.copytree(source, destination) will copy the


folder at the path source, along with all of its files and
subfolders, to the folder at the path destination.

 The source and destination parameters are both strings.


Copying Files and Folders

 The shutil.copytree() call creates a new folder named


spam_backup with the same content as the original spam folder.
Moving and Renaming Files and Folders
 Calling shutil.move(source, destination) will move the file or
folder at the path source to the path destination and will return
a string of the absolute path of the new location.

 If destination points to a folder, the source file gets moved into


destination and keeps its current filename.
Moving and Renaming Files and Folders

 The destination path can also specify a filename

 The folders that make up the destination must already exist, or


else Python will throw an exception
Permanently Deleting Files and Folders
 A single file or a single empty folder can be deleted with
functions in the os module

 The folder must be empty of any files or folders.

◦ Calling os.unlink(path) will delete the file at path

◦ Calling os.rmdir(path) will delete the folder at path

 shutil.rmtree(path) will remove the folder at path, and all files


and folders it contains will also be deleted.
Permanently Deleting Files and Folders
 The following Python code permanently deletes all files with
.rxt suffix
Safe Deletes with the send2trash Module
 send2trash will send folders and files to computer’s trash or
recycle bin instead of permanently deleting them

 The send2trash() function can only send files to the recycle


bin; it cannot pull files out of it.
Walking a Directory Tree
Walking a Directory Tree
 To walk through the directory tree, touching each file
os.walk() is used

 The os.walk() function will return three values on each


iteration through the loop:

◦ A string of the current folder’s name

◦ A list of strings of the folders in the current folder

◦ A list of strings of the files in the current folder


Walking a Directory Tree
Compressing Files with the zipfile
Module
Compressing Files with the zipfile
Module
 zip files can hold the compressed contents of many other files

 Compressing a file reduces its size, which is useful when


transferring it over the internet

 ZIP file can also package multiple files and subfolders into one

 This single file is called an archive file

 Python programs can create and open (or extract) ZIP files using
functions in the zip file module
Reading ZIP Files

 zipfile is the name of the Python module to be imported

 To read the contents of a ZIP file, first you must create a


ZipFile object(note the uppercase letters Z and F)

 To create a ZipFile object, call the zipfile.ZipFile() function,


passing it a string of the .ZIP file’s filename
Reading ZIP Files
 A ZipFile object has a namelist() method that returns a list of
strings for all the files and folders contained in the ZIP file

 These strings can be passed to the getinfo()ZipFile method to


return a ZipInfo object about that particular file

 a ZipInfo object holds useful information about a single file in the


archive such as file_size and compress_size in bytes, which hold
integers of the original file size and compressed file size, respectively
and so on
Reading ZIP Files

 The command at ➊
calculates how efficiently
example.zip is compressed

 The original file size is


divided by the compressed
file size
Extracting from ZIP Files
 The extractall() method for
ZipFile objects extracts all the
files and folders from a ZIP file
into the current working
directory
 A folder name can be passed to
extractall() to have it extract
the files into a folder other than
the current working directory
 If the folder passed to the
extractall() method does not
exist, it will be created
Extracting from ZIP Files
Extracting from ZIP Files

 The extract() method for ZipFile objects will extract a single


file from the ZIP file

 The string you pass to extract() must match one of the strings
in the list returned by namelist()

 A second argument can be passed to extract() to extract the


file into a folder other than the current working directory
Extracting from ZIP Files
 If this second argument is a folder that doesn’t yet exist, Python
will create the folder

 The value that extract() returns is the absolute path to which


the file was extracted
Creating and Adding to ZIP Files
 To create a compressed ZIP files, the ZipFile object must be
opened in write mode

 'w' should be passed as the second argument to ZipFile


function

 The write() method’s first argument is a string of the filename


to add

 The second argument is the compression type parameter,


which tells the computer what algorithm it should use to
compress the files
Creating and Adding to ZIP Files
 This value s is usually set to zipfile.ZIP_DEFLATED

 This specifies the deflate compression algorithm, which works


well on all types of data

 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

An assert statement says, “I assert that the condition holds


true, and if not, there is a bug somewhere, so immediately
stop the program.”
Disabling Assertions
 If you run a Python script with python -O myscript.py instead of
python myscript.py,
 Python will skip assert statements.
 Users might disable assertions when they’re developing a program
and need to run it in a production setting that requires peak
performance.
➢ ▪Assertions can be disabled by passing the -O option when running
Python.
➢ This is good for when you have finished writing and testing your
program and don’t want it to be slowed down by performing sanity
checks (although most of the time assert statements do not cause a
noticeable speed difference).
➢ Assertions are for development, not the final product.
➢ By the time you hand off your program to someone else to run, it
should be free of bugs
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.
Using the logging Module
 import logging
 logging.basicConfig(level=logging.DEBUG, format='
%(asctime)s - %(levelname)s - %(message)s')
The logging.disable() function disables these so that you don’t
have to go into your program and remove all the logging calls by
hand.
IDLE’s Debugger
 Debugging mode also adds the following
buttons to the top of the editor:
➢ Continue,
➢ Step Over,
➢ Step In, and
➢ Step Out.
➢ The usual Stop button is also available
Continue
 Clicking the Continue button will cause the
program to execute normally until it
terminates or reaches a breakpoint. If you are
done debugging and want the program to
continue normally, click the Continue button
Step In
 Clicking the Step In button will cause the
debugger to execute the next line of code and
then pause again. If the next line of code is a
function call, the debugger will “step into” that
function and jump to the first line of code of
that function.
Step Over
 Clicking the Step Over button will execute the
next line of code, similar to the Step In button.
However, if the next line of code is a function
call, the Step Over button will “step over” the
code in the function.The function’s code will
be executed at full speed, and the debugger
will pause as soon as the function call returns.
Step Out
 Clicking the Step Out button will cause the
debugger to execute lines of code at full speed
until it returns from the current function. If
you have stepped into a function call with the
Step In button and now simply want to keep
executing instructions until you get back out,
click the Out button to “step out” of the
current function call.
Stop
 If you want to stop debugging entirely and not
bother to continue executing the rest of the
program, click the Stop button. The Stop button
will immediately terminate the program.
 Stepping through the program with the debugger
is helpful but can also be slow. Often you’ll want
the program to run normally until it reaches a
certain line of code. You can configure the
debugger to do this with breakpoints.
Breakpoints
 A breakpoint can be seton a specific line of
code and forces the debugger to pause
whenever the program execution reaches that
line. Open a new file editor tab and enter the
following program, which simulates flipping a
coin 1,000 times. Save it as coinflip.py
When you run this program without the
debugger
If you ran this program under the
debugger
You could instead just set a breakpoint
on the line print('Halfway done!'

You might also like