Regular expressions
Regular Expressions, often shortened as regex, are
a sequence of characters used to check whether a
pattern exists in a given text (string) or not
‘Hi every one my contact no. is 444-123-4567’
To find a phone number in a string.
You know the pattern:
Three numbers, a hyphen, three numbers, a hyphen, and four
numbers.
Here’s an example:
415-555-4242
+91-9883443344
Finding Patterns of Text Without Regular
Expressions
def isPhoneNumber(text): if text[7] != '-':
if len(text) != 12: return False
return False for i in range(8, 12):
for i in range(0, 3): if not text[i].isdecimal():
if not text[i].isdecimal(): return False
return False return True
if text[3] != '-': print('415-555-4242 is a phone
return False number:')
for i in range(4, 7): print(isPhoneNumber('415-555-
if not text[i].isdecimal():
4242'))
return False print('Moshi moshi is a phone
415-555-4242 is a phone number:
True number:')
print(isPhoneNumber('Moshi
Moshi moshi is a phone number: moshi')
Finding Patterns of Text Without
Regular Expressions
def isPhoneNumber(text): if text[7] != '-':
if len(text) != 12: return False
return False for i in range(8, 12):
for i in range(0, 3): if not text[i].isdecimal():
if not return False
text[i].isdecimal(): return True
return False message = 'Call me at 415-555-1011
if text[3] != '-': tomorrow. 415-555-9999 is my office.'
return False for i in range(len(message)):
for i in range(4, 7): chunk = message[i:i+12]
if not if isPhoneNumber(chunk):
text[i].isdecimal(): print('Phone number
return False found: ' + chunk)
print('Done')
Finding Patterns of Text with Regular
Expressions
The isPhoneNumber() function is 17 lines but can find only one pattern of
phone numbers.
What about a phone number formatted like 415.555.4242 or (415) 555-
4242? Or 415-555-4242 x99?
The isPhoneNumber() function would fail to validate them.
Regular expressions, called regexes for short, are descriptions for a Pattern
of text.
For example, a \d in a regex stands for a digit character — that is, any single
numeral 0 to 9.
The regex \d\d\d-\d\d\d-\d\d\d\d is used.
A string of three numbers, a hyphen, three more
numbers, another hyphen, and four numbers.
A regular expressions can be much
more sophisticated.
[Link] a 3 in curly brackets
({3}) after a pattern is like saying,
“Match this pattern three times.”
regex \d{3}-\d{3}-\d{4}
\d\d\d-\d\d\d-\d\d\d\d
Creating Regex Objects
All the regex functions in Python are in the re module.
>>> import re
>>> phoneNumRegex = [Link](r'\d\d\d-\d\d\d-\d\
d\d\d’)
returns a Regex pattern object (or simply, a Regex
object).
The syntax involves backslash-escaped characters, and
to prevent these characters from being interpreted as
escape sequences; you use the raw r prefix.
Matching Regex Objects
1. Import the regex module with import re.
2. Create a Regex object with the [Link]() function. (use a raw
string.)
3. Pass the string you want to search into the Regex object’s search()
method. This returns a Match object.
4. Call the Match object’s group() method to return a string of the actual
matched text.
• compile function returns a Regex pattern object (or
simply, a Regex object).
• search function:- scan through the given string/sequence,
looking for the first location where the regular expression
produces a match.
• group function returns the string matched by the re
Example Program
import re
phoneNumRegex = [Link](r'\d\d\d-\d\d\d-\d\d\d\d')
mo = [Link]('My number is 415-555-4242
and 431-444-2421.')
print('Phone number found: ' + [Link]())
Output
Phone number found: 415-555-4242
More Pattern Matching with Regular
Expressions
Grouping with Parentheses
>>> phoneNumRegex = [Link](r'(\d\d\d)-(\d\d\d-\
d\d\d\d)')
>>> mo = [Link]('My
>>> [Link]() number is 415-
555-4242.') ('415', '555-4242')
>>> [Link](1) >>> areaCode, mainNumber =
'415' [Link]()
>>> [Link](2) >>> print(areaCode)
'555-4242' 415
>>> [Link](0) >>> print(mainNumber)
'415-555-4242' 555-4242
>>> [Link]()
'415-555-4242'
Adding parentheses will create groups in
the regex:
More Pattern Matching with Regular
Expressions
Matching Multiple Groups with the Pipe
The | character is called a pipe.
Use it anywhere you want to match one of many expressions.
>>> heroRegex = [Link] (r'Batman|Tina Fey')
>>> mo1 = [Link]('Batman and Tina
Fey.')
>>> [Link]()
'Batman'
>>> mo2 = [Link]('Tina Fey and
Batman.')
>>> [Link]()
'Tina Fey'
Use the pipe to match one of several patterns as part of
your regex.
>>> batRegex = [Link](r'Bat(man|mobile|copter|
bat)')
>>> mo = [Link]('Batmobile lost a wheel')
>>> [Link]()
'Batmobile'
>>> [Link](1)
'mobile'
[Link]() returns the full matched text 'Batmobile',
[Link](1) returns just the part of the matched text inside the
first parentheses group, 'mobile'
More Pattern Matching with
Regular Expressions
Optional Matching with the Question Mark
Sometimes there is a pattern that you want to match only
optionally.
That is, the regex should find a match whether or not that bit of
text is there.
The ? character flags the group that precedes it as an optional
>>> batRegex = [Link](r'Bat(wo)?man')
part of the pattern.
>>> mo1 = [Link]('The Adventures of
Batman')
>>> [Link]()
'Batman'
>>> mo2 = [Link]('The Adventures of
Batwoman')
>>> [Link]()
>>> phoneRegex = [Link](r'(\d\d\d-)?\d\d\
d-\d\d\d\d')
>>> mo1 = [Link]('My number
is 415-555-4242')
>>> [Link]()
'415-555-4242‘
>>> mo2 = [Link]('My number
is 555-4242')
>>> [Link]()
'555-4242'
More Pattern Matching with Regular
Expressions
Matching Zero or More with the Star
The * means “match zero or more”—the group that precedes the star
can occur any number of times in the text
It can be completely absent or repeated over and over again
>>> batRegex = [Link](r'Bat(wo)*man')
>>> mo1 = [Link]('The Adventures of Batman')
>>> [Link]()
'Batman'
>>> mo2 = [Link]('The Adventures of Batwoman')
>>> [Link]()
'Batwoman'
>>> mo3 = [Link]('The Adventures of
Batwowowowoman')
>>> [Link]()
'Batwowowowoman'
More Pattern Matching with Regular
Matching One or More with the Plus
Expressions
The + (or plus) means “match one or more.”
The group preceding a plus must appear at least once.
It is not optional.
>>> batRegex = [Link](r'Bat(wo)+man')
>>> mo1 = [Link]('The Adventures of
Batwoman')
>>> [Link]()
'Batwoman'
>>> mo2 = [Link]('The Adventures of
Batwowowowoman')
>>> [Link]()
'Batwowowowoman‘
>>> mo3 = [Link]('The Adventures of Batman')
More Pattern Matching with
Regular Expressions
Matching Specific Repetitions with Curly Brackets
Ifyou have a group that you want to repeat a specific
number of times, follow the group in your regex with a
number in curly brackets.
you can specify a range by writing a minimum, a comma,
and a maximum in between the curly brackets.
(Ha){3}
(Ha)(Ha)(Ha)
(Ha){3,5}
((Ha)(Ha)(Ha))|((Ha)(Ha)(Ha)(Ha))|((Ha)(Ha)(Ha)(Ha)(Ha))
More Pattern Matching with Regular
Expressions
Matching Specific Repetitions with Curly Brackets
>>> haRegex = [Link](r'(Ha){3}')
>>> mo1 = [Link]('HaHaHa')
>>> [Link]()
'HaHaHa'
>>> mo2 = [Link]('Ha')
>>> mo2 == None
True
Greedy and Nongreedy Matching
Python’s regular expressions are greedy by default, which means that in
ambiguous situations they will match the longest string possible.
>>> greedyHaRegex = [Link](r'(Ha){3,5}')
>>> mo1 = [Link]('HaHaHaHaHa')
>>> [Link]()
'HaHaHaHaHa‘
The nongreedy version of the curly brackets, which matches the shortest
string possible, has the closing curly bracket
>>> nongreedyHaRegex =
[Link](r'(Ha){3,5}?')
>>> mo2 =
[Link]('HaHaHa
HaHa')
>>> [Link]()
The findall() Method
search() will return a Match object of the first matched text in the
searched string,
The findall() method will return the strings of every match in the
searched string.
search() returns a Match object only on the first instance of matching text.
findall() will not return a Match object but a list of strings—as long as
there are no groups in the regular expression.
Each string in the list is a piece of the searched text that matched the
regular expression.
The findall() Method
>>> phoneNumRegex = [Link](r'\d\d\d-\d\d\d-\d\d\
d\d') # has no groups
>>> [Link]('Cell: 415-555-9999 Work:
212-555-0000')
['415-555-9999', '212-555-0000'] //a list of
strings
If there are groups in the regular expression, then findall() will
return a list of tuples
>>> phoneNumRegex = [Link](r'(\d\d\d)-(\d\d\d)-(\
d\d\d\d)') # has groups
>>> [Link]('Cell: 415-555-9999 Work:
212-555-0000')
[('415', '555', ‘9999'), ('212', '555', '0000')] //list of tuples
Character Classes
Shorthand character class Represents
\d Any numeric digit from 0 to 9.
\D Any character that is not a numeric digit from 0 to
9.
\w Any letter, numeric digit, or the underscore
character.
(Think of this as matching “word” characters.)
\W Any character that is not a letter, numeric digit, or
the
underscore character.
\s Any space, tab, or newline character. (Think of this
as
matching “space” characters.)
Character Classes
>>> xmasRegex = [Link](r'\d+\s\w+')
>>> [Link]('12 drummers, 11 pipers, 10
lords, 9 ladies, 8 maids, 7
swans, 6 geese, 5 rings, 4 birds, 3 hens, 2
doves, 1 artridge')
['12 drummers', '11 pipers', '10 lords', '9 ladies', '8 maids', '7
swans', ‘6 geese', '5 rings', '4 birds', '3 hens', '2 doves', '1
partridge']
\d+\s\w+ will match text that has one or more
• One or more numeric digits (\d+),
• A whitespace character (\s),
• one or more letter/digit/underscore characters (\w+).
The findall() method returns all matching strings of the regex
pattern in a list.
Making Your Own Character Classes
Define your own character class using square brackets[ ].
For example, the character class [a-z A-Z 0-9] will match all lowercase
letters, uppercase letters, and numbers
A negative character class will match all the characters that are not in the
character class by placing a caret character (^) just after the character
class’s opening bracket,
Making Your Own Character Classes
>>> vowelRegex =
[Link](r'[aeiouAEIOU]')
>>> [Link]('RoboCop eats baby
food. BABY FOOD.')
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']
>>> consonantRegex =
[Link](r'[^aeiouAEIOU]')
>>> [Link]('RoboCop eats
baby food. BABY FOOD.')
The Caret character
caret symbol (^) at the start of a regex to indicate that a match must occur
at the beginning of the searched text.
>>> beginsWithHello = [Link](r'^Hello')
>>> [Link]('Hello world!')
<[Link] object; span=(0, 5), match='Hello'>
>>> [Link]('He said hello.') == None
True
Dollar Sign Characters
dollar sign ($) at the end of the regex to indicate the
string must end with this regex pattern
>>> endsWithNumber = [Link](r'\d$')
>>> [Link]('Your number is 42')
<_sre.SRE_Match object; span=(16, 17),
match='2'>
>>> [Link]('Your number is forty two.') ==
None
True
The Caret and Dollar Sign Characters
And ^ and $ together to indicate that the entire string must match
the regex.
>>> wholeStringIsNum = [Link](r'^\d+$')
>>> [Link]('1234567890')
<_sre.SRE_Match object; span=(0, 10),
match='1234567890'>
>>> [Link]('12345xyz67890') ==
None
True
The Wildcard Character
The . (or dot) character in a regular expression is called a wildcard
and will match any one character except for a newline
>>> atRegex = [Link](r'.at')
>>> [Link]('The cat in the hat sat
on the flat mat.')
['cat', 'hat', 'sat', ‘lat', 'mat']
Matching Everything with Dot-
Star
Use the dot-star (.*) to stand in for that “anything.”
dot character means “any single character except the newline,”
star character means “zero or more of the preceding character.”
>>> nameRegex = [Link](r'First Name: (.*) Last
Name: (.*)')
>>> mo = [Link]('First Name: Reji Last
Name: Thomas')
>>> [Link](1)
‘Reji'
>>> [Link](2)
‘Thomas'
Matching Everything with Dot-Star
The dot-star uses greedy mode: It will always try to match as much
text as possible.
To match any and all text in a nongreedy fashion, use the dot, star,
and question mark (.*?).
>>> nongreedyRegex = [Link](r'<.*?>')
>>> mo = [Link]('<To serve
man> for dinner.>')
>>> [Link]()
'<To serve man>‘
>>> greedyRegex = [Link](r'<.*>')
>>> mo = [Link]('<To serve
man> for dinner.>')
>>> [Link]()
'<To serve man> for dinner.>'
Matching Newlines with the Dot
Character
The dot-star will match everything except a newline.
By passing [Link] as the second argument to [Link](),
make the dot character match all characters, including the newline
character.
>>> noNewlineRegex = [Link]('.*')
>>> [Link]('Serve the public trust.\nProtect
the innocent.\nUphold the law.').group()
'Serve the public trust.'
>>> newlineRegex = [Link]('.*', [Link])
>>> [Link]('Serve the public trust.\nProtect the
innocent.\nUphold the law.').group()
'Serve the public trust.\nProtect the innocent.\nUphold the law.'
Review of Regex Symbols
The ? matches zero or one of the preceding group.
The * matches zero or more of the preceding group.
The + matches one or more of the preceding group.
The {n} matches exactly n of the preceding group.
The {n,} matches n or more of the preceding group.
The {,m} matches 0 to m of the preceding group.
The {n,m} matches at least n and at most m of the preceding group.
{n,m}? or *? or +? performs a nongreedy match of the preceding group.
^spam means the string must begin with spam.
spam$ means the string must end with spam.
The . matches any character, except newline characters.
\d, \w, and \s match a digit, word, or space character, respectively.
\D, \W, and \S match anything except a digit, word, or space character,
respectively.
[abc] matches any character between the brackets (such as a, b, or c).
[^abc] matches any character that isn’t between the brackets.
Case-Insensitive Matching
Normally, regular expressions match text with the exact casing you
specify.
>>> regex1 = [Link]('RoboCop')
>>> regex2 = [Link]('ROBOCOP')
>>> regex3 = [Link]('robOcop')
>>> regex4 = [Link]('RobocOp')
To make regex case-insensitive, pass [Link] or re.I as a second
argument to [Link]().
>>> robocop = [Link](r'robocop', re.I)
>>> [Link]('RoboCop is part man, part
machine, all cop.').group()
'RoboCop'
>>> [Link]('ROBOCOP protects the
innocent.').group()
'ROBOCOP’
Substituting Strings with the sub() Method
Syntax: [Link] (pattern, repl, string, count=0, flags=0)
pattern –: pattern to be searched
repl- : pattern is replaced with repl
string –: original string
count-: checks and maintains the number of times this occurs.
Example
print([Link]('ub', '~*', 'Subject has Uber booked already',
flags=[Link]))
S~*ject has ~*er booked already
print([Link]('ub', '~*', 'Subject has Uber booked already'))
S~*ject has Uber booked already
print([Link]('ub', '~*', 'Subject has Uber booked already',
count=1, flags=[Link]))
S~*ject has Uber booked already
print([Link](r'\sAND\s', ‘ & ', 'Baked Beans And
Spam',flags=[Link])
Baked Beans & Spam
Substituting Strings with the
sub() Method
sub() is used to substitute new text in place of those
patterns.
The sub() method for Regex objects is passed two
arguments.
The first argument is a string to replace any
matches.
The second is the string for the regular expression.
The sub() method returns a string with the
substitutions applied.
>>> namesRegex = [Link](r'Agent \w+')
>>> [Link]('CENSORED', 'Agent Alice gave the secret
documents to Agent Bob.')
'CENSORED gave the secret documents to CENSORED.'
Substituting Strings with the
sub() Method
The matched text itself as part of the
substitution.
you can type \1, \2, \3, and so on, to mean “Enter
the text of group 1, 2, 3, and so on, in the
substitution.”
>>> agentNamesRegex = [Link](r'Agent (\w)\
w*')
>>> [Link](r'\1****', 'Agent Alice
told Agent Carol that Agent Eve knew Agent Bob was
a double agent.')
A**** told C**** that E**** knew B**** was a double agent.'
import re
agentNamesRegex = [Link](r'Agent (\w\w\w)(\w)\w*')
a=[Link](r’\1****', 'Agent Alice told Agent Carol
that Agent Eve knew Agent Bob was a double agent.')
print(a)
Ali**** told Car**** that Eve knew Bob was a double
agent.
import re
agentNamesRegex = [Link](r'Agent (\w\w\w)(\w)\w*')
a=[Link](r’\2****', 'Agent Alice told Agent Carol
that Agent Eve knew Agent Bob was a double agent.')
print(a)
c**** told o**** that Agent Eve knew Agent Bob was a
double agent.
Files and File Paths
A file has two key properties
1. filename -usually written as one word
2. Path.:- The path specifies the location of a file on
the computer
The Current Working Directory
Every program that runs on your computer has a current working
directory,
or cwd >>> import os
>>> [Link]()
'C:\\Python34'
>>> [Link]('C:\\Windows\\
System32')
>>> [Link]()
'C:\\Windows\\System32'
>>> [Link]('C:\\ThisFolderDoesNotExist')
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
[Link]('C:\\ThisFolderDoesNotExist')
FileNotFoundError: [WinError 2] The system cannot find the file
specified:
'C:\\ThisFolderDoesNotExist'
Absolute vs. Relative Paths
There are two ways to specify a file path.
1. • An absolute path, which always begins with the root folder
2. • A relative path, which is relative to the program’s current working
directory
A single period (“dot”) for a folder name is shorthand for
“this directory.”
Two periods (“dot-dot”) means “the parent folder.
Creating New Folders with [Link]
>>> import os
>>>[Link]('C:\\delicious\\walnut\\
waffles')
The [Link] Module
Checking Path Validity
The [Link] module provides functions to check whether a given path
exists and whether it is a file or folder.
Calling [Link](path) will return True if the file or folder referred to in
the argument exists and will return False if it does not exist.
Calling [Link](path) will return True if the path argument exists and is
a file and will return False otherwise.
Calling [Link](path) will return True if the path argument exists and is
a folder and will return False otherwise.
>>> [Link]('C:\\Windows')
True
>>> [Link]('C:\\some_made_up_folder')
False
>>> [Link]('C:\\Windows\\System32')
True
>>> [Link]('C:\\Windows\\System32')
False
>>> [Link]('C:\\Windows\\System32\\
[Link]')
False
>>> [Link]('C:\\Windows\\System32\\
[Link]')
True
Handling Absolute and Relative
Paths
Calling [Link](path) will return a string of the absolute path
of the argument. This is an easy way to convert a relative path into an
absolute one.
Calling [Link](path) will return True if the argument is an
absolute path and False if it is a relative path.
Calling [Link](path, start) will return a string of a relative
path
from the start path to path. If start is not provided, the current
working
directory is used as the start path.
Handling Absolute and Relative
Paths
>>> [Link]('.') >>> path = 'C:\\Windows\\System32\\
'C:\\Python34' [Link]'
>>> [Link]('.\\Scripts') >>> [Link](path)
'C:\\Python34\\Scripts' '[Link]'
>>> [Link]('.') >>> [Link](path)
False 'C:\\Windows\\System32'
>>> [Link]([Link]('.'))
True
>>> [Link]('C:\\Windows', 'C:\\')
'Windows'
>>> [Link]('C:\\Windows', 'C:\\spam\\eggs')
'..\\..\\Windows'
>>> [Link]()
'C:\\Python34'
Finding File Sizes and Folder
Contents
>>> [Link]('C:\\Windows\\System32\\[Link]')
776192
>>> [Link]('C:\\Windows\\System32')
['0409', '[Link]', '[Link]', '[Link]',
'[Link]',
--snip--
'[Link]', '[Link]', 'zh-CN', 'zh-HK', 'zh-TW', '[Link]']
The File Reading/Writing
Process
There are three steps to reading or writing files in Python.
1. Call the open() function to return a File object.
2. Call the read() or write() method on the File object.
3. Close the file by calling the close() method on the File
object
Opening Files with the open()
Function
To open a file with open() function, pass a string path indicating the
file to open
It can be either an absolute or relative path.
The open() function returns a File object.
Windows OS
helloFile = open('C:\\Program Files (x86)\\python27\\[Link]’)
OS X
helloFile = open('/Users/your_home_folder/[Link]')
File Content in [Link]
When, in disgrace with fortune and men's eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself and curse my fate,
Reading the Contents of Files
File object, is used to start reading the file.
To read the entire contents of a file as a string value, use the File object’s
read()
readlines() method is used to get a list of string values from the file
>>> helloContent =
[Link]()
>>> helloContent
>>> a 'Hello world!'
= open('C:\\Program Files (x86)\\python27\\
[Link]')
>>> [Link]()
["When, in disgrace with fortune and men's eyes,\n", 'I
all alone beweep my outcast state,\n', 'And trouble deaf
heaven with my bootless cries,\n', 'And look upon myself
and curse my fate,']
Writing to Files
Python allows to write content to a file in a way similar to how the
print() function “writes” strings to the screen.
You can’t write to a file you’ve opened in read mode, though.
Instead, you need to open it in “write plaintext (w)” mode or
“append plaintext (a)” mode, or write mode and append mode
for short.
Write mode will overwrite the existing file and start from scratch.
Writing to Files
>>> baconFile = open('[Link]', 'w')
>>> [Link]('Hello world!\n')
13 >>> print(content)
Hello world!
>>> [Link]()
Bacon is not a vegetable.
>>> baconFile = open('[Link]', 'a')
>>> [Link]('Bacon is not a vegetable.')
25
>>> [Link]()
>>> baconFile = open('[Link]')
>>> content = [Link]()
>>> [Link]()
Saving Variables with the
shelve Module
You can save variables in your Python programs to binary shelf files
using the shelve module.
The shelve module implements persistent storage for arbitrary Python
objects which can be pickled, using a dictionary
This way, program can restore data to variables from the hard drive.
The shelve module will let you add Save and Open features to your program.
For example, if you ran a program and entered some configuration
settings, you could save those settings to a shelf file and then have the
program load them the next time it is run.
import shelve import shelve
s = [Link](‘reji’)
try:
s['key1'] = { ‘Hello reji s=
How are you' } [Link](‘reji')
finally: try:
[Link]() existing =
s['key1']
Out put finally:
{'hello reji how are you'}
[Link]()
print(existing)
Saving Variables with the [Link]()
Function
The [Link]() function will “pretty print” the contents of a
list or dictionary to the screen.
While the [Link]() function will return this same text as a
string instead of printing it.
Not only is this string formatted to be easy to read, but it is also
syntactically correct Python code.
Example :
You have a list of dictionary stored in a variable and you want to
save this variable and its contents for future use.
[Link]() will give you a string that you can write
to .py file.
Saving Variables with the [Link]()
Function
>>> import pprint
>>> cats = [{'name': 'Zophie', 'desc': 'chubby'}, {'name':
'Pooka', 'desc': 'fluffy'}] //list of dictionaries
>>> [Link](cats)
"[{'desc': 'chubby', 'name': 'Zophie'}, {'desc': 'fluffy', 'name':
'Pooka'}]"
>>> fileObj = open('[Link]', 'w')
>>> [Link]('cats = ' + [Link](cats) + '\n')
83
>>> [Link]()
>>> import myCats
>>> [Link]
[{'name': 'Zophie', 'desc': 'chubby'}, {'name': 'Pooka', 'desc':
'fluffy'}]
>>> [Link][0]
{'name': 'Zophie', 'desc': 'chubby'}
>>> [Link][0]['name']
'Zophie'
Project: Generating Random
Quiz Files
Creates 35 different quizzes.
Creates 50 multiple-choice questions for each quiz, in random order.
Provides the correct answer and three random wrong answers for each question, in random
order.
Writes the quizzes to 35 text files.
Writes the answer keys to 35 text files.
This means the code will need to do the following:
Store the states and their capitals in a dictionary.
Call open(), write(), and close() for the quiz and answer key text files.
Use [Link]() to randomize the order of the questions and multiple- choice options.
Project: Generating Random
Quiz Files
Step 1: Store the Quiz Data in a Dictionary
Step 2: Create the Quiz File and Shuffle the Question Order
Step 3: Create the Answer Options
Step 4: Write Content to the Quiz and Answer Key Files
Project: Multiclipboard
The command line argument for the keyword is checked.
If the argument is save, then the clipboard contents are saved to the keyword.
If the argument is list, then all the keywords are copied to the clipboard.
Otherwise, the text for the keyword is copied to the keyboard.
This means the code will need to do the following:
Read the command line arguments from [Link].
Read and write to the clipboard.
Save and load to a shelf file.
Project: Multiclipboard
Step 1: Comments and Shelf Setup
Step 2: Save Clipboard Content with a Keyword
Step 3: List Keywords and Load a Keyword’s Content
The shutil Module
The shutil (shell utilities) module has functions to copy, move,
rename, and delete files in your Python programs.
To use the shutil functions use import shutil.
The shutil Module
1. Copying Files and Folders
>>> import shutil, os
>>> [Link]('C:\\') >>> import shutil, os
>>> [Link]('C:\\[Link]', 'C:\\ >>> [Link]('C:\\')
delicious') >>> [Link]('C:\\bacon', 'C:\\
'C:\\delicious\\[Link]‘ bacon_backup')
>>> [Link]('[Link]', 'C:\\delicious\\ 'C:\\bacon_backup'
[Link]')
'C:\\delicious\\[Link]'
The shutil Module
1. Copying Files and Folders
The shutil module provides functions for copying files, as well as entire folders.
Calling [Link](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.
[Link](source, destination) will copy an entire folder and every
folder and file contained in it.
Calling [Link] will copy the folder at the path source, along with all of
its files and subfolders, to the folder at the path destination.
The shutil Module
2. Moving and Renaming Files and Folders
Calling [Link](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
>>> [Link]('C:\\[Link]',
'C:\\eggs')
'C:\\eggs\\[Link]'
The shutil Module
3. Permanently Deleting Files and Folders
Delete a file or a empty folder with functions in the os module,
whereas to delete a folder and all of its contents, use the
shutil module.
Calling [Link](path) will delete the file at path.
Calling [Link](path) will delete the folder at path. This folder
must be empty of any files or folders.
Calling [Link](path) will remove the folder at path, and all files
and folders it contains will also be deleted.
The shutil Module
3. Permanently Deleting Files and Folders
import os
for filename in [Link]():
if [Link]('.rxt'):
print(fliename)
[Link](filename)
The shutil Module
Python’s built-in [Link]() function irreversibly deletes
files
and folders, it can be dangerous to use.
4. Safe Deletes with the send2trash Module
A better way to delete files and folders is with the third-party
send2trash module.
Install this module by running pip install send2trash from a
Terminal window.
Using send2trash sends file to computer’s trash or recycle bin
The shutil Module
4. Safe Deletes with the send2trash Module
>>> import send2trash
>>> baconFile = open('[Link]', 'a') #
creates the file
>>> [Link]('Bacon is not a
vegetable.')
25
>>> [Link]()
>>> send2trash.send2trash('[Link]')
Compressing Files with the
zipfile Module
ZIP files can hold the compressed contents of many other files.
Compressing a file reduces its size.
ZIP file can also contain multiple files and subfolders, it’s a handy
way to package several files into one.
This single file, called an archive file, can then be, say, attached
to an email.
Python programs can both create and open (or extract) ZIP files
using functions in the zipfile module.
Compressing Files with the
zipfile Module
1. Reading ZIP Files
To read the contents of a ZIP file, first you must create a ZipFile
object
ZipFile objects are conceptually similar to the File objects returned by
the open() function
To create a ZipFile object, call the [Link]() function, passing
it a string of the .zip file’s filename.
zipfile is the name of the Python module, and ZipFile() is the name
of the function.
Compressing Files with the zipfile Module
[Link] ZIP Files
>>> import zipfile, os
>>> [Link]('C:\\') # move to the folder with
[Link]
>>> exampleZip = [Link]('[Link]')
>>> [Link]() #gives list of strings for all
the files and folders
['[Link]', 'cats/', 'cats/[Link]', 'cats/[Link]']
>>> spamInfo = [Link]('[Link]')
>>> spamInfo.file_size
13908
>>> spamInfo.compress_size
3828
>>> [Link]()
Compressing Files with the zipfile
Module
2. 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.
The extract() method for ZipFile objects will extract a single file from the ZIP file
>>> import zipfile, os >>>
>>> [Link]('C:\\') [Link]('spam.t
>>> exampleZip = xt')
[Link]('[Link]') 'C:\\[Link]'
>>> [Link]()
>>> [Link]()
Compressing Files with the zipfile
Module
4. Creating and Adding to ZIP Files
To create your own compressed ZIP files, open the ZipFile object in
write mode by passing 'w' as the second argument.
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;
zipfile.ZIP_DEFLATED- This specifies the deflate compression
algorithm
Compressing Files with the zipfile
Module
4. Creating and Adding to ZIP Files
>>> import zipfile
>>> newZip = [Link]('[Link]', 'w')
>>> [Link]('[Link]',
compress_type=zipfile.ZIP_DEFLATED)
>>> [Link]()
Project: Renaming Files with American-Style Dates
to
European-Style Dates
American-style dates (MM-DD-YYYY) in their names and needs them renamed to
European-style dates (DD-MM-YYYY).
TO DO
It searches all the filenames in the current working directory for American-style dates.
When one is found, it renames the file with the month and day swapped to make it European-style.
This means the code will need to do the following:
Create a regex that can identify the text pattern of American-style dates.
Call [Link]() to find all the files in the working directory.
Loop over each filename, using the regex to check whether it has a date.
If it has a date, rename the file with [Link]().
Project: Backing Up a Folder into a ZI
P File
Step 1: Figure Out the ZIP File’s Name
Step 2: Create the New ZIP File
Step 3: Walk the Directory Tree and Add to the ZIP File
Project: Renaming Files with American-Style Dates
to
European-Style
Step 1: Create Dates
a Regex for American-Style Dates
Step 2: Identify the Date Parts from the Filenames
Step 3: Form the New Filename and Rename the Files
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.
Raising Exceptions
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
>>> raise Exception('This is the error
message.')
Traceback (most recent call last):
File "<pyshell#191>", line 1, in <module>
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 error
information called the traceback.
The traceback includes
the error message,
the line number of the line that caused the error,
the sequence of the function calls that led to the error.
This sequence of calls is called the call stack.
Traceback (most recent call
last):
File "[Link]", line 7,
in <module>
spam()
File "[Link]", line 2,
in spam
bacon()
Getting the Traceback as a String
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().
import Python’s traceback module before calling this function.
Instead of crashing program right when an exception occurs, write
the traceback information to a log file and keep program running.
look at the log file later, when ready to debug program.
Getting the Traceback as a String
>>> import traceback
>>> try:
raise Exception('This is the error message.')
except:
errorFile = open('[Link]', 'w')
[Link](traceback.format_exc())
[Link]()
print('The traceback info was written to
[Link].')
116
The traceback info was written to [Link].
Assertions
1. An assertion is a sanity-check .
2. Programmers often place assertions at the start of
a function to check for valid input, and after a
function call to check for valid output.
3. you can turn on or turn off when you are done with
your testing of the program.
The syntax for assert is
assert Expression[,
Arguments]
An assert statement consists of the following:
1. The assert keyword
2. A condition (that is, an expression that
evaluates to True or False)
3. A comma
4. A string to display when the condition is
False
def KelvinToFahrenheit(Temperature):
assert (Temperature >= 0),"Colder than absolute zero!"
return ((Temperature-273)*1.8)+32
print KelvinToFahrenheit(273)
print int(KelvinToFahrenheit(505.78))
print KelvinToFahrenheit(-5)
32.0
451
Traceback (most recent call last):
File "[Link]", line 9, in <module>
print KelvinToFahrenheit(-5)
File "[Link]", line 4, in KelvinToFahrenheit
assert (Temperature >= 0),"Colder than absolute zero!"
AssertionError: Colder than absolute zero!
Assertions
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
your program and don’t want it to be slowed down by
performing sanity checks
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 and not require the sanity
checks.
Logging
Logging is a great way to understand what’s happening in your program
and in what order its happening.
print() is one way to know.
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.
Logging
Using the logging Module
To enable the logging module to display log messages on your
screen as add the following to the top of your program your
import logging
program runs
[Link](level=[Link], format=' %(asctime)s - %
(levelname)s - %(message)s')
When Python logs an event, it creates a LogRecord object that holds
information about that event.
The logging module’s basicConfig() function lets you specify what
details about the LogRecord object you want to see and how you
want those details displayed.
Using the logging Module
import logging
[Link](level=[Link], format=' %
(asctime)s - %(levelname)s %(message)s')
[Link]('Start of program')
def factorial(n):
[Link]('Start of factorial(%s%%)' % (n))
total = 1
for i in range(n + 1):
total *= i
[Link]('i is ' + str(i) + ', total is ' +
str(total))
[Link]('End of factorial(%s%%)' % (n))
return total
print(factorial(5))
[Link]('End of program')
2021-12-21 22:45:50,013 - DEBUG - Start of program
2021-12-21 22:45:50,016 - DEBUG - Start of
factorial(5%)
2021-12-21 22:45:50,020 - DEBUG - i is 0, total is 0
2021-12-21 22:45:50,021 - DEBUG - i is 1, total is 0
2021-12-21 22:45:50,022 - DEBUG - i is 2, total is 0
2021-12-21 22:45:50,023 - DEBUG - i is 3, total is 0
2021-12-21 22:45:50,023 - DEBUG - i is 4, total is 0
2021-12-21 22:45:50,024 - DEBUG - i is 5, total is 0
2021-12-21 22:45:50,025 - DEBUG - End of factorial(5)
0
2021-12-21 22:45:50,029 - DEBUG - End of program
Logging messages provide a trail of breadcrumbs that
can help you figure out when things started to go
wrong.
Logging
Don’t Debug with print()
Typing import logging and
[Link](level=[Link], format= '%
(asctime)s - %(levelname)s - %(message)s') is somewhat
unwieldy.
You might even accidentally remove some print() calls that were being
used for nonlog messages.
The nice thing about log messages is that you’re free to fill your
program with as many as you like, and you can always disable them
later by adding a single [Link]([Link]) call.
Unlike print(), the logging module makes it easy to switch between
showing and hiding log messages.
Log messages are intended for the programmer, not the user.
Logging
Level Logging Function Description
DEBUG [Link]() The lowest level. Used
for small details. Usually you
care about these messages only
when diagnosing problems.
INFO [Link]() Used to record information on
general
events in your program or
confirm that
things are working at their
point in the
program.
WARNING [Link]() Used to indicate a
potential problem that doesn’t
Level Logging Function Description
ERROR [Link]() Used to record an
error that caused the
program to fail to do something.
CRITICAL [Link]() The highest
level. Used to indicate a
fatal error that has caused or is
about to cause the program to
stop running entirely.
Logging
Logging Levels
>>> import logging
>>> [Link](level=[Link],
format=' %(asctime)s -%(levelname)s %
(message)s')
>>> [Link]('Some debugging details.')
2015-05-18 19:04:26,901 - DEBUG - Some debugging
details.
>>> [Link]('The logging module is working.')
2015-05-18 19:04:35,569 - INFO - The logging module is
working.
>>> [Link]('An error message is about to
be logged.')
2015-05-18 19:04:56,843 - WARNING - An error message
is about to be logged.
Logging
Disabling Logging
Afterprogram has debugged, you probably don’t want all
these log messages.
The [Link]() function disables these so that
you don’t have to go into your program and remove all
the logging calls by hand.
So if you want to disable logging entirely, just add
[Link]([Link]) to your program
Logging
Disabling Logging
>>> import logging
>>> [Link](level=[Link], format=' %(asctime)s -%
(levelname)s - %(message)s')
>>> [Link]('Critical error! Critical error!')
2015-05-22 11:10:48,054 - CRITICAL - Critical error! Critical error!
>>> [Link]([Link])
>>> [Link]('Critical error! Critical error!')
>>> [Link]('Error! Error!')
Logging
Logging to a File
Instead of displaying the log messages to the screen, you can write them to a text
file.
The [Link]() function takes a filename keyword argument
>>> import logging
>>> [Link](filename='[Link]',
level=[Link], format='
%(asctime)s - %(levelname)s - %(message)s')
IDLE ’s Debugger
The debugger is a feature of IDLE that allows you to execute program one line at a time.
The debugger will run a single line of code and then wait for continue information.
To enable IDLE’s debugger, click Debug>Debugger in the interactive shell window.
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, the debugger will pause execution before the
first instruction and display the following:
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
IDLE ’s Debugger
The program will stay paused until you press one of the five buttons in the Debug
Control window:
Go
Clicking the Go 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 Go button.
Step
Clicking the Step button will cause the debugger to execute the next line of code and then
pause again.
Over
Clicking the Over button will execute the next line of code. 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 call returns
IDLE ’s Debugger
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
If you want to run your program normally again, select
Debug>Debugger again to disable the debugger.
IDLE ’s Debugger
Debugging a Number Adding Program
When you press F5 or select Run4Run Module (with Debug4Debugger
enabled and all four checkboxes on the Debug Control window checked), the
program starts in a paused state on line 1
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)
IDLE ’s Debugger
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.
To set a breakpoint, right-click the line in the file editor and select Set
Breakpoint.