Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Traceback As A String, Assertions, Logging, IDLE S Debugger

Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

Module-4

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.
The shutil Module (or) shell utilities

shutil module has functions to copy, move,


rename, and delete files in our Python
programs.

To use the shutil functions,We need to use

import shutil
shutil.copy('C:\\Users\\girishns\\Desktop\\src\\abc.txt',

'C:\\Users\\girishns\\Desktop\\dest\\ds.txt')

Shutil.copy(‘C:\\src\\file1.txt’, ’C:\\dest\\file2.txt’)
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.

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.


>>> import shutil

>>>shutil.copy('C:\\spam.txt', 'C:\\folder1’)

'C:\\folder1\\spam.txt‘

>>> shutil.copy('eggs.txt‘, ‘C:\\folder1\\eggs2.txt')

'C:\\folder1\\eggs2.txt'
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.

The source and destination parameters are both


strings.
>>> import shutil

>>> shutil.copytree('C:\\folder1', 'C:\\ folder2 _backup')

'C:\\ folder2_backup'
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.
>>> import shutil

>>> shutil.move('C:\\file1.txt', 'C:\\eggs')

'C:\\eggs\\file1.txt'

File1 .txt is moved to eggs folder!!!!


Rename:

The source file is moved and renamed.

shutil.move('C:\\abc.txt‘ , 'C:\\fldr\\new_abc.txt')

'C:\\fldr\\new_abc.txt'
Permanently Deleting Files and Folders

We 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, we use the


shutil module.

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

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


folder must be empty of any files or folders.

• Calling shutil.rmtree(path) will remove the folder at path,


and all files and folders it contains will also be deleted
Import os
os.unlink(‘xyz.txt’)
Safe Deletes with the send2trash Module

A much better way to delete files and folders is


with the third-party send2trash module.

We can install this module by running


pip install send2trash from a Terminal window

import send2trash
send2trash.send2trash(‘abc.txt')
Walking a Directory Tree
If we want to rename every file in some folder
and also every file in every subfolder of that
folder.

That is, we want to walk through the directory


tree, touching each file as you go.
The os.walk() function is passed a single string
value: the path of a folder.

We can use os.walk() in a for loop statement to


walk a directory tree.
the os.walk() function will return three values on
each iteration through the loop:

1.A string of the current folder’s name

2. A list of strings of the folders in the current


folder.

3. A list of strings of the files in the current


folder.
Walking a Directory Tree
import os
for folderName, subfolders, filenames in os.walk('C:\\x'):
print('The current folder is ' + folderName)
for subfolder in subfolders:
print('SUBFOLDER OF ' + folderName + ': ' + subfolder)
for filename in filenames:
print('FILE INSIDE ' + folderName + ': '+ filename)
print('')

The current folder is C:\x


SUBFOLDER OF C:\x: y
The current folder is C:\x\y
SUBFOLDER OF C:\x\y: z
The current folder is C:\x\y\\z
FILE INSIDE C:\x\y\z: abc.txt
Zip extraction
demo
!!!!!!!!!!!!!!!!!!!!!!!!1
example.zip
You can download this ZIP file from

http:// nostarch.com/automatestuff/
Compressing Files with the zipfile Module
ZIP files (with the .zip file extension), which 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 contain multiple files and subfolders of single


file, called an archive file and then it can be attached to an
email.

Python program can both create and open (or extract) ZIP files
using functions in the zipfile module
Reading ZIP Files

To read the contents of a ZIP file, We must create a


ZipFile object.

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


function, passing it a string of the .zip file’s filename.

Note that zipfile is the name of the Python module &

ZipFile() is the name of the function.


example=zipfile.ZipFile('C:\\Users\\girishns\\Desktop
\\src\\xyz.zip')
xyz
>>> example.namelist()

['xyz/a/', 'xyz/a/f.txt', 'xyz/bb/', 'xyz/cd.txt'] a

f.txt

A ZipFile object has a namelist() cd.txt


method that returns a list of strings bb
for all the files and folders contained
in the ZIP file.
>>>example=zipfile.ZipFile('C:\\Users\\girishns\\Desktop\\src\\xyz.zip')

>>>example.namelist()
['xyz/a/', 'xyz/a/f.txt', 'xyz/bb/', 'xyz/cd.txt']

fileino = example.getinfo(‘f.txt')
ZipInfo objects have
>>> fileinfo.file_size their own attributes, such
as file_size and
13908 compress_size in bytes,
which hold integers of the
>>> fileinfo.compress_size original file size and
3828 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.

>>> import zipfile,os


>>> os.chdir('C:\\') # move to the zip folder
>>> example = zipfile.ZipFile('example.zip')
>>> example.extractall()
>>> exampleZip.close()
Creating and Adding to ZIP Files

To create our own compressed ZIP files, we must open


the ZipFile object in write mode by passing 'w' as the
second argument.

>>> import zipfile


>>> file = zipfile.ZipFile('new.zip', 'w')

>>> file.write(‘add.txt', compress_type=zipfile.ZIP_DEFLATED)

>>> file.close()
file = zipfile.ZipFile('new.zip', 'w')
Parameters
file: This argument should be a filename or the path
to a zip file.

mode:This argument defines the mode in which the


zip file needs to be accessed.There are four modes:
'r':This mode is used to read an existing file.
'w':This mode is used to write to a file.
'a':This mode is used to append to a file.

compression:This argument stores the compression


method for the zip file.
Project: Renaming Files with American-Style Dates to
European-Style Dates

left ……………….!
Project: Backing Up a Folder into a ZIP File
Step 1: Figure Out the ZIP File’s Name The code for this
program will be placed into a function named backupToZip().

Step 2: Create the New ZIP File.

Step 3: Walk the Directory Tree and Add to the ZIP File.
import zipfile, os

def backupToZip(folder):
folder = os.path.abspath(folder) # make sure folder is absolute
number = 1
while True:
zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'
if not os.path.exists(zipFilename):
break
number = number + 1
print('Creating %s...' % (zipFilename))
backupZip = zipfile.ZipFile(zipFilename, 'w')
for foldername, subfolders, filenames in os.walk(folder):
print('Adding files in %s...' % (foldername))
backupZip.write(foldername)
for filename in filenames:
if filename.startswith(os.path.basename(folder) + '_') and filename.endswith('.zip'):
continue
backupZip.write(os.path.join(foldername, filename))
backupZip.close()
print('Done.')
backupToZip('C:\\delicious')
Debugging:
Raising Exceptions,
Getting the Traceback as a String,
Assertions,
Logging,
IDLE’s Debugger (Integrated Development and
Learning Environment ).
A debugger is a program that can
help you find out what is going on in
a computer program
The Best Tools to Debug Python
Sentry. Sentry is an error-tracking and performance-
monitoring tool that can diagnose and resolve python
problems. ...
Pycharm. Pycharm is a python-focused IDE featuring
some of the greatest python debugging tools available.
Rollbar. ...
Instabug. ...
Visual Studio. ...
Raygun. ...
Glitchtip. ...
Komodo IDE.
Raising Exceptions

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
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
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 the error, and
the sequence of the function calls that led to the
error.

This sequence of calls is called the call stack


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().

def spam():
bacon()
def bacon():
raise Exception('This is the error message.')
spam()

Traceback (most recent call last): File "errorExample.py",


line 7, in spam() File "errorExample.py", line 2, in spam
bacon() File "errorExample.py", line 5, in bacon raise
Exception('This is the error message.') Exception: This is the
error message
when an exception occurs, we can write the traceback
information to a log file and keep our program
running.
>>> import traceback
>>> try: raise Exception('This is the error message.')
except:
errorFile = open('errorInfo.txt', 'w')
errorFile.write(traceback.format_exc())
errorFile.close()
print('The traceback info was written to errorInfo.txt.')

116
The traceback info was written to errorInfo.txt
Assertions
It is a debugging tool, and its primary task is to check
the condition. If it finds that the condition is true, it
moves to the next line of code, and If not, then stops
all its operations and throws an error.

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
# initializing number
x=7
y=0
# It uses assert to check for 0
print ("x / y value is : ")
assert y != 0, "Divide by 0 error"
print (x / y)

Traceback (most recent call last):


File "main.py", line 6, in <module>
assert y != 0, "Divide by 0 error"
AssertionError: Divide by 0 error
Disabling Assertions Assertions can be disabled
by passing the -O option when running Python.

This is good for when you have finished writing


and testing our program
Logging
Logging is the process of writing information into log
files. Log files contain information about various
events that happened in operating system, software, or
in communication.

Logging is done for the following purposes:


Information gathering
Troubleshooting
Generating statistics
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
our screen.

import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.debug('Start of program')
num=5
sum = 0
while(num > 0):
sum = sum+num
logging.debug('num is ' + str(num) + ', total is ' + str(sum))
num = num-1
print("The sum is", sum)
logging.debug('End of program')

2022-12-07 11:08:16,744 - DEBUG - Start of program


2022-12-07 11:08:16,757 - DEBUG - num is 5, total is 5
2022-12-07 11:08:16,767 - DEBUG - num is 4, total is 9
2022-12-07 11:08:16,783 - DEBUG - num is 3, total is 12
2022-12-07 11:08:16,795 - DEBUG - num is 2, total is 14
2022-12-07 11:08:16,810 - DEBUG - num is 1, total is 15
The sum is 15
Logging Levels
There are five logging levels

Detailed information, for


diagnosing problems. Value=10

Confirm things are working as


expected. Value=20

Something unexpected happened,


or indicative of some problem. But
the software is still working as
expected. Value=30
More serious problem, the software is not
able to perform some function. Value=40

A serious error, the program itself


may be unable to continue
running. Value=50
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s -%(levelname)s - %(message)s')

logging.debug('This is a debug message')

logging.info('This is an info message')

logging.warning('This is a warning message')

logging.error('This is an error message')

logging.critical('This is a critical message')


2022-12-07 11:32:01,872 -DEBUG - This is a debug message

2022-12-07 11:32:01,889 -INFO - This is an info message

2022-12-07 11:32:01,903 -WARNING - This is a warning message

2022-12-07 11:32:01,903 -ERROR - This is an error message

2022-12-07 11:32:01,919 -CRITICAL - This is a critical message


Disabling Logging

The logging.disable() function disables these levels so


that we don’t have to go into our program and
remove all the logging calls by hand.

logging.critical('Critical error! Critical error!')

2015-05-22 11:10:48,054 - CRITICAL - Critical error! Critical error!

>>> logging.disable(logging.CRITICAL)
Logging to a File

Instead of displaying the log messages to the


screen, we can write them to a text file.

The logging.basicConfig() function takes a


filename as an argument.
import logging

logging.basicConfig(filename='myLog.txt', level=logging.DEBUG,
format=' %(asctime)s - %(levelname)s - %(message)s')
IDLE’s Debugger

The debugger is a feature of IDLE that allows us


to execute our program one line at a time.

To enable IDLE’s debugger, click Debug >


Debugger in the interactive shell window.

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.
It displays the following information
The line of code that is about to be executed

A list of all local variables and their values

A list of all global variables and their values


The five buttons in the Debug Control window: Go, Step,
Over, Out, or Quit.

Go Clicking the Go button will cause the program to


execute normally until it terminates or reaches a
breakpoint.

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.
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.

Out Clicking the Out button will cause the debugger to


execute lines of code at full speed until it returns from
the current function.

Quit If you want to stop debugging entirely and not


bother to continue executing the rest of the program,
click the Quit button.
Debugging a Number Adding Program
print('Enter the first number to add:')
first = input()

print('Enter the second number to add:')


second = input()

print('Enter the third number to add:')


third = input()

print('The sum is ' + first + second + third)

DEMO LEFT ………….


Breakpoints A breakpoint can be set on a
specific line of code and forces the
debugger to pause whenever the program
execution reaches that line.

You might also like