Python Progr Module 3 - 6th EC by 21EC643
Python Progr Module 3 - 6th EC by 21EC643
SURESHA V
Python
Programming
MODULE 3
Pattern Matching with
Regular Expressions
&
Reading and Writing Files
Professor
Dept. of E&CE
K.V.G. College of Engineering, Sullia, D.K-574 327
Affiliated to Visvesvaraya Technological University
Belagavi - 590018
Python Programming (21EC643) - Module 3:
Pattern Matching with Regular Expressions & Reading and Writing Files
MODULE 3
Pattern Matching with Regular Expressions & Reading and Writing Files
Learning Outcomes
After reading this Module, the student will be able to:
o Understand the foundational concepts of regular expressions, including literals,
metacharacters, and escape sequences.
o Learn the syntax of more pattern matching with regular expressions.
o Understand the different functions and methods for managing file paths,
including relative and absolute paths.
o Learn how to open files in different modes (read, write, append).
o Read and write data from files using various methods.
o Create and manage file content effectively, including appending data to existing
files.
o Three more numeric characters then another hyphen , and finally four more
numbers .
o If the program execution manages to get past all the checks, it returns True
o Calling isPhoneNumber() with the argument '415-555-4242' will return True.
o Calling isPhoneNumber() with 'Moshi moshi' will return False; the first test
fails because 'Moshi Moshi' is not 12 characters long.
Program output:
Review of Regular Expression Matching *** : There are several steps to using
regular expressions in Python, each step is fairly simple.
1. Import the regex module with import re.
2. Create a Regex object with the re.compile() function.
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.
7.3 More Pattern Matching with Regular Expressions: Some of the more powerful
pattern-matching capabilities are
1. Grouping with Parentheses
2. Matching Multiple Groups with the Pipe
3. Optional Matching with the Question Mark
4. Matching Zero or More with the Star
5. Matching One or More with the Plus
6. Matching Specific Repetitions with Curly Brackets
o Since mo.groups() returns a tuple of multiple values, we can use the multiple-
assignment trick to assign each value to a separate variable, as in the previous
areaCode, mainNumber = mo.groups() line.
o You can use the pipe symbol "|" to match one of several patterns as part of your
regex. For example, to match any of the strings 'Batman', 'Batmobile', 'Batcopter',
and 'Batbat', you can specify the common prefix 'Bat' only once using ( )
o The method call mo.group() returns the full matched text 'Batmobile', while
mo.group(1) returns just the part of the matched text inside the first parentheses
group, 'mobile'.
o By using the pipe character and grouping parentheses, we can specify several
alternative patterns you would like your regex to match.
o The (wo)? part of the regular expression means that the pattern wo is an
optional group.
o The regex will match text that has zero instances or one instance of (wo) in it.
o This is why the regex matches both 'Batwoman' and 'Batman'.
o For 'Batman', the (wo)* part of the regex matches zero instances of wo in the
string; for 'Batwoman', the (wo)* matches one instance of (wo); and for
'Batwowowowoman', (wo)* matches FOUR instances of (wo).
o 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.
o Example:
o Character classes are good for shortening regular expressions. The character
class [0-5] will match only the numbers 0 to 5; this is much shorter than typing
(0|1|2|3|4|5).
o The regular expression \d+\s\w+ will match text that has one or more numeric
digits (\d+), followed by a whitespace character (\s), followed by one or more
letter/digit/underscore characters (\w+). The findall() method returns all
matching strings of the regex pattern in a list.
o Note: The dot character will match just one character, which is why the match for
the text flat in the previous example matched only lat.
o The dot-star (.*) uses greedy mode: It will always try to match as much text as
possible.
o The dot, star, and question mark (.*?): To match any and all text.
o Enter the following into the interactive shell to see the difference between the
greedy and nongreedy versions:
8.1 Intorduction
o Variables in the programs are store data while program is running.
o But if we want our data to persist even after our program has finished, we need
to save it to a file.
o Python also supports file handling and allows users to handle files, i.e. read and
write files, along with many other file handling options.
8.2 Files and File Paths
What is File?
- File is a named location on disk to store related information.
- File is an object on a computer that stores data, information, settings, or
commands used with a computer program.
- A file has two key properties: a filename and a path.
- The part of the filename after the last period is called the file’s extension and
tells you a file’s type.
- Example: project.docx is a Word document.
os.path.join() function:***
o The os.path.join() function is helpful to create strings for filenames.Example
o If you os.path.join() will return a string with a file path using the correct path
separators. The following example joins names from a list of filenames to the end
of a folder’s name:
o Example:
Absolute vs. Relative Paths: There are two ways to specify a file path.
1. Absolute path:
It always begins with the root folder.
Absolute paths ensure that Python can find the exact file on your
computer.
Example: D:\User\Python program\chapter 1\files1.txt
2. Relative path:
It is relative to the program’s current working directory(cwd).
relative paths hold less information than absolute paths
For example:..\home\sally\statusReport.txt
o This will create not just the C:\Suresh folder but also a python folder inside C:\
and module 2 folder inside folder python
o Finally C:\Suresh\python\module 2 new folder is created with absolute path.
o For example, enter the following into the interactive shell to find base and
directory name.
os.path.split()function
o os.path.split() method in Python is used to split the path name into a
pair root and extension.
o os.path.split() to get a tuple value with directory and base strings. It is a nice
shortcut if you need both values.
o Example:
o Note:os.path.split() does not take a file path and returns a list of strings of
each folder.
o For that, use the split() string method and split on the string in os.path.sep.
Recall from earlier that the os.sep variable is set to the correct folder-
separating slash for the computer running the program.Example
o The split() string method will work to return a list of each part of the path. It
will work on any operating system if you pass it os.path.sep.
• os.listdir(path):It will return a list of filename strings for each file in the
path argument.
Example:
2. Reading the Contents of Files: Following Functions are used for read
operations.
i. read(): reading the content of the file.
ii. read(integer): Read Only Parts of the File, can also specify how many
characters you want to return
iii. readline(): Read one line of the file, calling 2 times to read two lines.
iv. readlines(): read all lines from the file.
read(): reading the content of the file. I have stored the content in the file
hello.txt
read(+ integer): Read Only Parts of the File, can also specify how many characters
you want to return
Syntax Program Output
open
( filename,
mode )
o If the filename passed to open() does not exist, both write and append mode will
create a new, blank file.
o After reading or writing a file, call the close() method before opening the file
again .
o Example Write Mode:
Syntax Program Output
open
( filename,
mode )
o while the pprint.pformat() function will return this same text as a string instead
of printing it.
o Pprint (pretty print) is a Python module used for formatting complex data
structures more readably and organized, especially when printing them to the
console or writing to a file.
o Using pprint.pformat() will give you a string that you can write to .py file.
o This file will be your very own module that you can import whenever you want
to use the variable stored in it.
o Example:
Acknowledgement:
My sincere thanks to the author Al Sweigart, because the above contents are prepared from his
textbook “Automate the Boring Stuff with Python: Practical Programming for Total
Beginners”
Prepared by:
Dr. Suresha V
Professor & Principal
Dept. of Electronics and Communication Engineering.
Reach me at: [email protected]
WhatsApp: +91 8310992434