Module - 3
Module - 3
Organizing Fi les
Organizing Fi les
• Your programs can also organize preexisting files on the hard drive. Maybe
you’ve had the experience of going through a folder full of dozens, hundreds,
or even thousands of files and copying, renaming, moving, or compressing
them all by hand.
The shutil Module:
The shutil (or shell utilities) module has functions to let you copy, move,
rename, and delete files in your Python programs.
To use the shutil functions, you will first need to use import shutil.
Copying Files and Folders
The shutil module provides functions for copying files, as well as entire
folders.
Calling shutil.copy(source, destination) will copy the file at the path source to
the folder at the path destination. (Both source and destination are strings.)
If destination is a filename, it will be used as the new name of the copied file.
This function returns a string of the path of the copied file.
Cont..
• Enter the following into the interactive shell to see how shutil.copy() works:
>>> os.chdir('C:\\')
'C:\\delicious\\spam.txt'
'C:\\delicious\\eggs2.txt'
The first shutil.copy() call copies the file at C:\spam.txt to the folder C:\delicious. The return
value is the path of the newly copied file.
Note that since a folder was specified as the destination, the original spam.txt filename is
used for the new, copied file’s filename.
The second shutil.copy() call also copies the file at C:\eggs.txt to the folder C:\delicious but
gives the copied file the name eggs2.txt.
While shutil.copy() will copy a single file, 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.
Cont..
The source and destination parameters are both strings. The function returns a string of the
path of the copied folder.
>>> os.chdir('C:\\')
'C:\\bacon_backup‘
The shutil.copytree() call creates a new folder named bacon_backup with the same content
as the original bacon folder. You have now safely backed up your precious, precious bacon.
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. For example, enter the following into the interactive
shell:
'C:\\eggs\\bacon.txt'
Assuming a folder named eggs already exists in the C:\ directory, this
shutil.move() calls says, “Move C:\bacon.txt into the folder C:\eggs.”
If there had been a bacon.txt file already in C:\eggs, it would have been
overwritten. Since it’s easy to accidentally overwrite files in this way, you
should take some care when using move().
The destination path can also specify a filename. In the following example,
'C:\\eggs\\new_bacon.txt‘
Cont..
• Both of the previous examples worked under the assumption that there was a
folder eggs in the C:\ directory. But if there is no eggs folder, then move() will
rename bacon.txt to a file named eggs.
'C:\\eggs'
• Here, move() can’t find a folder named eggs in the C:\ directory and so
assumes that destination must be specifying a filename, not a folder. So the
bacon.txt text file is renamed to eggs.
Permanently Deleting Files and Folders
You can delete a single file or a single empty folder with functions in the os module,
whereas to delete a folder and all of its contents, you use the shutil module.
• Calling os.rmdir(path) will delete the folder at path. This folder must be empty of
• Calling shutil.rmtree(path) will remove the folder at path, and all files and folders
A much better way to delete files and folders is with the third-party
send2trash module. You can install this module by running pip install
send2trash from a Terminal window.
• After you have installed send2trash, enter the following into the interactive
shell:
>>> import send2trash
>>> baconFile = open('bacon.txt', 'a') # creates the file
>>> baconFile.write('Bacon is not a vegetable.')
25
>>> baconFile.close()
>>> send2trash.send2trash('bacon.txt')
Walking a Directory Tree
• Say you want to rename every file in some folder
and also every file in every subfolder of that
folder. That is, you want to walk through the
directory tree, touching each file as you go.
Writing a program to do this could get tricky;
fortunately, Python provides a function to handle
this process for you.
• The os.walk() function is passed a single string value: the path of a folder. You
can use os.walk() in a for loop statement to walk a directory tree, much like
how you can use the range() function to walk over a range of numbers.
Cont..
Unlike range(), the os.walk() function will return three values on each iteration
through the loop:
• ZipFile objects are conceptually similar to the File objects you saw returned
by the open() function in the previous chapter: They are values through
which the program interacts with the file.
• ZipInfo objects have their own attributes, such as file size and compress_size in
bytes, which hold integers of the original file size and compressed file size,
respectively.
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.
>>> exampleZip.extractall()
>>> exampleZip.close()
After running this code, the contents of example.zip will be extracted to C:\.
Optionally, you can pass a folder name 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.
For instance, if you replaced the call at with
exampleZip.extractall('C:\\delicious'), the code would extract the files from
example.zip into a newly created C:\delicious folder.
The extract() method for ZipFile objects will extract a single file from the
ZIP file. Continue the interactive shell example:
>>> exampleZip.extract('spam.txt')
'C:\\spam.txt'
>>> exampleZip.extract('spam.txt', 'C:\\some\\new\\folders')
'C:\\some\\new\\folders\\spam.txt'
>>> exampleZip.close()
The string you pass to extract() must match one of the strings in the list
returned by namelist(). Optionally, you can pass a second argument to
extract() to extract the file into a folder other than the current working
directory.
Creating and Adding to ZIP Files
To create your own compressed ZIP files, you must open the ZipFile object
in write mode by passing 'w' as the second argument.
When you pass a path to the write() method of a ZipFile object, Python
will compress the file at that path and add it into the ZIP file.
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; you can always
just set this value to zipfile.ZIP_DEFLATED.
• Enter the following into the interactive shell:
>>> newZip.close()
• This code will create a new ZIP file named new.zip that has the compressed
contents of spam.txt.
Debugging
• The debugger is a feature of IDLE that executes a program one
instruction at a time, giving you a chance to inspect the values in
variables while your code runs, and track how the values change over
the course of your program.
Raising Exceptions
• Python raises an exception whenever it tries to execute invalid code.
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.
For example, open a new file editor window, enter the following code, and
save the program as boxPrint.py:
def boxPrint(symbol, width, height):
if len(symbol) != 1:
raise Exception('Symbol must be a single character string.')
if width <= 2:
raise Exception('Width must be greater than 2.')
if height <= 2:
raise Exception('Height must be greater than 2.')
print(symbol * width)
for i in range(height - 2):
print(symbol + (' ' * (width - 2)) + symbol)
print(symbol * width)
for sym, w, h in (('*', 4, 4), ('O', 20, 5), ('x', 1, 3), ('ZZ', 3, 3)):
try:
boxPrint(sym, w, h)
except Exception as err:
print('An exception happened: ' + str(err))
Getting the Traceback as a String
The traceback includes the error message, the line number of the line
that caused the error, and the sequence of the function calls that led to
the error. This sequence of calls is called the call stack.
Cont..
Open a new file editor window in IDLE, enter the
def spam():
bacon()
def bacon():
spam()
like this:
• The traceback is displayed by Python whenever a raised exception goes
unhandled. But you can also obtain it as a string by calling
traceback.format_exc(). This function is useful if you want the information
from an exception’s traceback but also want an except statement to
gracefully handle the exception.
The 116 is the return value from the write() method, since 116 characters were
written to the file
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 Assertion Error exception is raised. In code, an
assert statement consists of the following:
• A comma
These two variables will be for the intersections of Market Street and 2nd Street,
• 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
• 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
import logging
• The logging module’s basicConfig() function specify the details about the
LogRecord object.
factorialLog.py
The output of this program looks like this:
• Change the for i in range(n + 1): line to for i in range(1, n + 1):, and run the
program again. The output will look like this:
Logging Levels
argument will show messages from all the logging levels (DEBUG being the
lowest level).
will show only ERROR and CRITICAL messages and skip the DEBUG, INFO,
• You simply pass logging.disable() a logging level, and itwill suppress all
log messages at that level or lower. So if you want to disable logging
entirely, just add logging.disable(logging.CRITICAL) to your program.
Cont..
>>> import logging
>>> logging.basicConfig(level=logging.INFO, format=' %(asctime)s -
%(levelname)s - %(message)s')
>>> logging.critical('Critical error! Critical error!')
2015-05-22 11:10:48,054 - CRITICAL - Critical error! Critical error!
>>> logging.disable(logging.CRITICAL)
>>> logging.critical('Critical error! Critical error!')
>>> logging.error('Error! Error!')
Logging to a File
• Instead of displaying the log messages to the screen, you can write them to a text
file. The logging.basicConfig() function takes a filename keyword argument.
>>import logging
Step
Clicking the Step button will cause the debugger to execute the next line of code and then pause again. The
Debug Control window’s list of global and local variables will be updated if their values change. 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.
Cont..
Over
• Clicking the Over button will execute the next line of code, similar to the Step
button. However, if the next line of code is a function call, the 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 all returns. For example, if the next line of code is a print()
call, you don’t really care about code inside the built-in print() function;
Out
• Clicking the 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
button and now simply want to keep executing instructions until you get back out, click the
Quit
• If you want to stop debugging entirely and not bother to continue executing the rest of the
program, click the Quit button. The Quit button will immediately terminate the program. If
you want to run your program normally again, select Debug4Debugger again to disable the
debugger.
Practice Questions
1. What is the difference between shutil.copy() and shutil.copytree()?
2. What function is used to rename files?
3. What is the difference between the delete functions in the send2trash
and shutil modules?
4. ZipFile objects have a close() method just like File objects’ close()
method. What ZipFile method is equivalent to File objects’ open()
method?