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

Python Module-3 QB Solution (21EC643)

Python solutions (21EC643)

Uploaded by

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

Python Module-3 QB Solution (21EC643)

Python solutions (21EC643)

Uploaded by

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

PYTHON PROGRAMMING

Module -3
Question Bank Solution

1. Explain the finding patterns of text without regular expressions with example.

Soln: Finding Phone Number from a Given Text without using regular expression> instead
using if else statements

Example:
To find phone numbers with the format (XXX-XXX-XXXX) within a given text.
The phone number format consists of 12 characters: the first 3 are digits, followed by a hyphen,
then 3 digits, another hyphen, and finally 4 digits.

Program:
def PhoneNumber(text):
if len(text) != 12:
return False
for i in range(0, 3):
if not text[i].isdecimal():
return False
if text[3] != '-':
return False
for i in range(4, 7):
if not text[i].isdecimal():
return False
if text[7] != '-':
return False
for i in range(8, 12):
if not text[i].isdecimal():
return False
return True.

#function call
text= “415-555-4242 ”
Z=PhoneNumber(text)
Output : True

text= “Dhoni-123 ”
Z=PhoneNumber(text)
Output : False

Explanation
1. Function Definition: PhoneNumber
2. Check if the string length is 12 characters. If not, return False.
3. Verify the first 3 characters are digits. If not, return False.
4. Check if the 4th character is a hyphen ('-'). If not, return False.
5. Verify characters 5 to 7 are digits. If not, return False.
6. Check if the 8th character is a hyphen ('-'). If not, return False.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

7. Verify characters 9 to 12 are digits. If not, return False.


8. Output: If all are True, return True.

Finding Phone Numbers in a Larger Text


• Loop Through the String: Iterate through message, check each of 12-character
• Check Each Chunk: If a chunk matches the phone number pattern, print it.

message = 'Call me at 415-555-1011 tomorrow. 415-555-9999 is my office.'

for i in range(len(message)):
chunk = message[i :i+12]
if PhoneNumber(chunk):
print(f 'Phone number found: {chunk}’)

Limitations of Previous Approach:


The PhoneNumber() function is long and only matches one specific pattern.
It doesn't work for other formats like 415.555.4242 or (415) 555-4242 or extensions like
415-555-4242 x99.

With example, explain the following Pattern Matching with Regular Expressions.

i. Grouping with Parentheses


ii. Matching Multiple Groups with the Pipe
iii. Pipe with parenthesis or Matching patterns with a common prefix
iv. Question Mark (?) [Optional Matching
v. Star(*) (Matching zero or More)
vi. Difference Between * and ? in Regex
vii. Plus (+) (Matching One or More)
viii. Caret(^) symbol
ix. Dollar($)
x. Combination of caret and dollar sign
xi. Wild card character (.)

Soln:

i. Grouping with Parentheses

o Parentheses () in regex is used to define groups.


o Parenthesis is used to create sub-patterns within the larger pattern to obtain specific parts,
separately.
Creating Groups:
o Putting parentheses () around parts of the regex pattern creates groups.
o Example: (\d\d\d)-(\d\d\d-\d\d\d\d) creates two groups:
(\d\d\d)=> group 1
(\d\d\d-\d\d\d\d)=> group 2

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

Accessing Groups:
o After performing a search with search() method, use group() method to retrieve
specific sub groups:
▪ mo.group(0)( (equivalent to mo.group()) retrieves the entire matched text.
▪ mo.group(1) retrieves the matched first group.
▪ mo.group(2) retrieves matched second group.

Program:

import re

#store the string in variable


str= "My phone number is : 233-567-8989"

#define the pattern


pattern=r"(\d\d\d)-(\d\d\d-\d\d\d\d)"

#compile the pattern


compl=re.compile(pattern)

#search the pattern using search method


mo=compl.search(str)

#retrive the mo using group()


Entire_no=mo.group(0)if mo else “no match”
Area_code=mo.group(1) if mo else “no match”
Main_no=mo.group(2) if mo else “no match”

#print result
print(f"Area code : {area_code}\n Main number : {main_number} \nEntire
number : {entire_no}")

Pipe Character (|): Matching Multiple Groups using Pipe

o The | character is used to match one of many objects in the text or string.
o It is kind of OR operation.
o Syntax: r 'Ramesh | Mahesh'

It will match either 'Ramesh' or 'Mahesh' in the string

o If both 'Ramesh' and 'Mahesh' are in the string, the first occurrence is matched.

import re
Str1='Ramesh Mahesh Suresh Kalmesh'
Str2='Mahesh Ramesh Suresh Kalmesh'

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

pattern = r'Ramesh|Mahesh'
comp= re.compile(pattern)

mo1=comp.search(Str1)
mo1_g= mo1.group() if mo1 else “No match”
print(mo1_g) # Output: Ramesh

mo2=comp.search(Str2)
mo2_g= mo2.group() if mo2 else “No match”
print(mo2_g) # Output: Mahesh

Pipe with parenthesis Matching patterns with a common prefix


• To match patterns with a common prefix, we have to use parentheses.
• Example:

'Batman', 'Batmobile', 'Batcopter', and 'Batbat'

Pattern= r'Bat(man|mobile|copter|bat)'.

• This way, we can specify the common prefix 'Bat' only once.

Program:

import re

# Store the string with common prefix


Str = 'Batman, Batmobile, Batcopter, and Batbat'

# Define the r pattern with common prefix


pattern = r'Bat (man|mobile|copter|bat)'

# Compile the regex pattern


comp = re.compile(pattern)

# Use the search method to search for the pattern


mo1 = comp.search(Str)

# Retrieve the full matched text and the part inside parentheses
mo1_g = mo1.group() # Full match
mo1_g2 = mo1.group(3) # Part inside parentheses

# Print the results


print(mo1_g) # Output: Batman (the first full match)
print(mo1_g2) # Output: copter (part of the match inside parentheses)

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

Question Mark (?) [Optional Matching (zero or one)]


• Question mark (?) character is used for optional matching in the regex.
• The group is created using parenthesis. The group followed by ? is considered as optional
• Here group has may NOT appear or if it is appearing, it has to appear only once, not multiple
times.
• Example:
• Pattern: r'Bat(wo)?man'
Here (wo) is group and is optional.
• Str1 = 'The Adventures of Batman' ; Output: Batman
• Str2= 'The Adventures of Batwoman'; Output: Batwoman
• Str2= 'The Adventures of Batwowoman' ; Output: None

matches both 'Batman' (without 'wo') and 'Batwoman' (with 'wo').

import re

# Store the string with possible matches


Str1 = 'The Adventures of Batman'
Str2= 'The Adventures of Batwoman'
Str3= 'The Adventures of Batwowoman'

# Define the pattern with optional matching (?)


pattern = r'Bat(wo)?man'

# Compile the regex pattern


comp = re.compile(pattern)

# Use the search method to search for the pattern


mo1 = comp.search(Str1)
mo1_g = mo1.group() if mo1 else “no match”
print(mo1_g) # Output: Batman

mo2 = comp.search(Str2)
mo2_g = mo2.group() if mo2 else “no match”
print(mo2_g) # Output: Batwoman

Mo3 = comp.search(Str3)
Mo3_g = mo3.group() if mo3 else “no match”
print(mo3_g) # Output: None

except :
print(“No match”)

Example -2
import re

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

# Store the string in a variable


String1 = 'My number is 415-555-4242.'
String2= 'My number is 555-4242.'

# Store the regex pattern in a variable


pattern = r'(\d{3}-)?\d{3}-\d{4}'

# Compile the regex pattern


compl = re.compile(pattern)

try:

#for string 1
# Use the search method to search for the pattern
mo1 = compl.search(String1)
mo1_g=mo1.group() if mo1 else “no match”
print(mo1_g)

mo2 = compl.search(String2)
mo2_g=mo2.group()
print(mo2_g)

except:
print(“None”)

Star(*) (Matching zero or More)


• The star (*) is used to match zero or more instances.
• Here group can appear any number of times, or not at all.

Example: r' Bat(wo)*man' can match:


• 'Batman' (zero instances of 'wo')
• 'Batwoman' (one instance of 'wo')
• 'Batwowowowoman' (multiple instances of 'wo')
import re

# Store the string with possible matches


Str1 = 'The Adventures of Batman'
Str2 = 'The Adventures of Batwoman'
Str3 = 'The Adventures of Batwowowowoman'

# Define the pattern with zero or more matching (*)


pattern = r'Bat(wo)*man'

# Compile the regex pattern


comp = re.compile(pattern)

try:
# Use the search method to search for the pattern
mo1 = comp.search(Str1)
mo1_g = mo1.group() if mo1 else “no match”

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

print(mo1_g) # Output: Batman

mo2 = comp.search(Str2)
mo2_g = mo2.group() if mo2 else “no match”
print(mo2_g) # Output: Batwoman

mo3 = comp.search(Str3)
mo3_g = mo3.group() if mo3 else “no match”
print(mo3_g) # Output: Batwowowowoman

except:
print(“No match”)

Difference Between * and ? in Regex


• * (star): Matches zero or more instances of the preceding group.
o Example: r'Bat(wo)*man' can match 'Batman', 'Batwoman', 'Batwowowowoman', etc.

• ? (question mark): Matches zero or one instance of the preceding group.


o Example: r'Bat(wo)?man' can match 'Batman' or 'Batwoman', but not 'Batwowowowoman'.

Plus (+) (Matching One or More)


• + (plus): Matches one or more objects in the string
• The group having a plus must appear at least once.
• If we need to match an actual plus sign character, then prefix the plus sign with a backslash to escape it:
\+.

import re

# Store the string with possible matches


Str1 = 'The Adventures of Batman'
Str2 = 'The Adventures of Batwoman'
Str3 = 'The Adventures of Batwowowowoman'

# Define the pattern with (+)


pattern = r'Bat(wo)+ man'

# Compile the regex pattern


comp = re.compile(pattern)

# Use the search method to search for the pattern


mo1 = comp.search(Str1)
mo1_g = mo1.group() if mo1 else ‘No match’
print(mo1_g) # Output: No match

mo2 = comp.search(Str2)
mo2_g = mo2.group() if mo1 else ‘No match’
print(mo2_g) # Output: Batwoman

mo3 = comp.search(Str3)
mo3_g = mo3.group() if mo1 else ‘No match’
print(mo3_g) # Output: Batwowowowoman

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

Caret (^) symbols

• It is used at the beginning of a regex pattern.


• It indicates that the match must occur at the start of the text.
• Example: r'^Hello' matches strings that start with 'Hello'.

import re
Str1='Hello world!'
Str2='He said hello.'

Pattern =r'^Hello'
comp= re.compile(pattern)

#text starting with pattern


mo1=comp.search(Str1)
mo1_g= comp. group() if mo1 else ‘No match’
print(mo1_g) Output: Hello

mo2=comp.search(Str2)
mo2_g= comp. group() if mo2 else ‘No match’
print(mo2_g) Output: ‘None’

Dollar ($) Sign Symbol:

• It is used at the end of a regex pattern.


• It indicates that the match must occur at the end of the text.
• Example: r'$Hello$' matches strings or text that ends with 'Hello'.

import re
Str1='Hello world!'
Str2='He said Hello'

Pattern =r 'Hello$'
comp= re.compile(pattern)

mo1=comp.search(Str1)
mo1_g= mo1. group() if mo1 else ‘None’
print(mo1_g) Output: Hello

mo2=comp.search(Str2)
mo2_g= mo2. group() if mo2 else ‘None’
print(mo2_g) Output: ‘None’

Combining Caret (^) and Dollar ($) :

• It indicates, that the entire string must match the pattern.


• Example: r'^\d+$' matches strings that consist the entirely of digits.

import re

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

Str1='1234567890'
Str2='12345xyz67890'

Pattern = r'^\d+$'
comp= re.compile(pattern)

mo1=comp.search(Str1)
mo1_g= mo1. group() if mo1 else ‘No match’
print(mo1_g) Output: 1234567890

mo2=comp.search(Str2)
mo2_g= mo2. group() if mo1 else ‘No match’
print(mo2_g) Output: No match

▪ These symbols are essential for defining precise patterns in text matching using regular
expressions.

The Wildcard Character (.) [DOT character ]

• The dot character (.) can be used to match any single character in a string, such as a letter,
number, or symbol.
• Pattern a.b would match aab, a9b, a-b, or any three-character string where the first character
is 'a', the second is any character, and the third is 'b'.

import re

# Store the string in a variable


text = 'The cat in the hat sat on the flat mat.'

# Define pattern to match any character followed by 'at'


pattern = r' .at' # looks for 3 letter set, any 1st and 2nd, 3rd character to be “at”

# Compile the pattern


comp = re.compile(pattern)

# Use findall method to find all matches


mo = comp.findall(text)
print(mo) # Output: ['cat', 'hat', 'sat', 'lat', 'mat']

Dot-Star (.*) [Matches Everything]


▪ dot means “matches any single character ”
▪ star means “matching zero or more
▪ (.*) matches zero or more any character except a newline.
▪ Used to match any text.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

Example:
import re

# Store the string in a variable


text = 'First Name: Virat Last Name: Kohli'

# Define pattern t
pattern = r'First Name: (.*) Last Name: (.*)'

# Compile the pattern


comp = re.compile(pattern)

# Use search method to find the match


mo = comp.search(text)
mo_g1 = mo.group(1) if mo else 'No match'
mo_g2 = mo.group(2) if mo else 'No match'

print(mo_g1) # Output: 'Virat'


print(mo_g2) # Output: 'Kohli'

Write a Python program that uses regular expressions to find all instances of a number followed by
a word in a given string. Your program should output these matches. Use the following string for
testing:
Str = '12 drummers, 11 pipers, 10 lords, 9 ladies, 8 maids, 7 swans, 6
geese, 5 rings, 4 birds, 3 hens, 2 doves, 1 partridge'

import re

# Define the string to search


Str = '12 drummers, 11 pipers, 10 lords, 9 ladies, 8 maids, 7 swans, 6
geese, 5 rings, 4 birds, 3 hens, 2 doves, 1 partridge'

# Define the regex pattern using shorthand character classes


pattern = r'\d+\s+\w+'

# Compile the regex pattern


comp = re.compile(pattern)

# Use findall method to find all matches


matches = comp.findall(Str)

# Print the results


print(matches) # Output: ['12 drummers', '11 pipers', '10 lords', '9
ladies', '8 maids', '7 swans', '6 geese', '5 rings', '4 birds', '3
hens', '2 doves', '1 partridge']

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

In this example, the regular expression \d+\s+\w+ matches text with one or more numeric digits
(\d+), followed by a whitespace character (\s), followed by one or more word characters (\w+). The
findall() method returns all matching objects of string.

Write a Python program that uses re.VERBOSE to find phone numbers in a given text. The
program should recognize various phone number formats, including those with area codes in
parentheses, different separators, and optional extensions. Use the following text for testing:

text = 'Call me at (123) 456-7890 or 123-456-7890 ext. 1234.'

▪ Regular expressions can get complicated when dealing with complex text patterns.
▪ To make them more readable, we can use "verbose mode" with re.VERBOSE, which allows,
whitespace and comments inside the regex string.

Benefits of Verbose Mode:


• Readability: Regex pattern can spread over multiple lines.
• Comments: We can add comments to explain parts of the regex.
• Whitespace Ignored: Spaces and newlines within the regex are ignored.

Program:
import re

# String or Text
text = 'Call me at (123) 456-7890 or 123-456-7890 ext. 1234.'

# Define the regex pattern with comments and spread over multiple lines
pattern = r'''(
(\d{3} | \(\d{3}\))? # area code
(\s |- |\.)? # separator
\d{3} # first 3 digits
(\s |- |\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)'''

# Compile the pattern with re.VERBOSE


comp = re.compile(pattern, re.VERBOSE)

# Find matches in the text


matches = comp.findall(text)

# Print matches
for match in matches:
print(match)

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

For the given a string, text = 'India won the twenty twenty world cup 2024'.
Write a Python program to perform the following tasks using regular expressions (regex):
i. Create and compile a regex pattern to find all vowels (both lowercase and uppercase) in the
given string.
ii. Create and compile a regex pattern to find all alphanumeric characters (both letters and
numbers) in the given string

import re

# Store the string in a variable


text = 'India won the twenty twenty world cup'

# Define pattern
Pattern1 = r'[aeiouAEIOU]'
Pattern2 = r'[a-zA-Z0-9]'

# Compile the pattern


Comp1= re.compile(Pattern1)
Comp2= re.compile(Pattern2)

#for pattern1
Mo1 = Comp1.findall(text)
print(Mo1) # Output: ['I', 'i', 'a', 'o', 'e', 'e', 'e', 'o', 'u']

#for pattern2
Mo2 = Comp2.findall(text)
print(Mo2)
# Output: 'I’n’d’I’a’ w’o’n’ t’h’e’ t’w’e’n’t’y’ t’w’e’n’t’y’ w’o’r’l’d’ c’u’p'

Explain regex functions sub(), re.IGNORECASE, re.DOTALL, and re.VERBOSE

[re.DOTALL] Matching Newlines with the Dot Character

The dot-star will match everything except a newline.


By passing re.DOTALL as the second argument to re.compile(), we can make the dot character to
match all characters, including the newline character

Program
import re

# Store the string in a variable


text = 'Serve the public trust.\nProtect the innocent.\nUphold the law.'

# Define pattern (does not match newlines)


pattern1 = r'.*'

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

# Compile the pattern


comp1 = re.compile(pattern1)

# Use search method to find the match


mo1 = comp1.search(text)
mo_g= mo1.group() if mo1 else 'No match'
print(mo_g) # Output: 'Serve the public trust.'

# Define pattern with re.DOTALL (matches newlines)


pattern2 = r '.*'

# Compile the pattern with re.DOTALL


comp2 = re.compile(pattern2, re.DOTALL)

# Use search method to find the match


mo2 = comp2.search(text)
mo2_g = mo2.group() if mo2 else 'No match'
print(mo2_g) # Output: 'Serve the public trust.\nProtect the innocent.\nUphold the law.'

(re.IGNORECASE or re.I) Case-Insensitive Matching


o Regular expressions normally match text exactly as specified in terms of casing.
o To match text regardless of uppercase or lowercase letters, use re.IGNORECASE or re.I as
the second argument in re.compile().

Program:
import re

# Define text with different casings


text1 = 'RoboCop is artificial police'
text2 = 'ROBOCOP protects the innocent.'
text3 = 'Why you talk so much about robocop ?'

# Define pattern for case-insensitive matching


pattern = r'robocop'

# Compile the pattern with re.IGNORECASE


comp = re.compile(pattern, re.IGNORECASE)

# Perform searches and print results


mo1 = comp.search(text1)
mo2 = comp.search(text2)
mo3 = comp.search(text3)

result1 = mo1.group() if mo1 else 'No match'


result2 = mo2.group() if mo2 else 'No match'
result3 = mo3.group() if mo3 else 'No match'

print(result1) # Output: 'RoboCop'


print(result2) # Output: 'ROBOCOP'

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

print(result3) # Output: 'robocop'

sub() Method [To replace obtained pattern with new string]


The sub() method is used to find patterns in text and then it replaces matched object them with new
text.
Example:

Program :Replace all instances of "Agent [name]" with "CENSORED"

import re

# text or string
text = 'Agent Virat gave the secret documents to Agent Dhoni.'

# Define the pattern


Pattern= r 'Agent \w+ '

comp = re.compile(pattern)

# Use the sub() method to replace matched patterns


result = comp.sub('CENSORED', text)

print(result)
# Output: 'CENSORED gave the secret documents to CENSORED.'

Managing Complex Regexes [re.VERBOSE]


▪ Regular expressions can get complicated when dealing with complex text patterns.
▪ To make them more readable, we can use "verbose mode" with re.VERBOSE, which allows,
whitespace and comments inside the regex string.

Program:
import re

# String or Text
text = 'Call me at (123) 456-7890 or 123-456-7890 ext. 1234.'

# Define the regex pattern with comments and spread over multiple lines
pattern = r'''(
(\d{3} | \(\d{3}\))? # area code
(\s |- |\.)? # separator
\d{3} # first 3 digits
(\s |- |\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)'''

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

# Compile the pattern with re.VERBOSE


comp = re.compile(pattern, re.VERBOSE)

# Find matches in the text


matches = comp.findall(text)

# Print matches
for match in matches:
print(match)

Combining re.IGNORECASE, re.DOTALL, and re.VERBOSE


▪ We can use re.VERBOSE for comments and readability, and re.IGNORECASE for case
insensitivity, we can combine these multiple flags using the bitwise OR operator (|).
▪ This allows us to include multiple options in the re.compile() function.

Steps to Combine Flags:

1. Combine Flags: Use the bitwise OR operator (|) to combine re.IGNORECASE, re.DOTALL, and
re.VERBOSE.
2. Compile with Combined Flags: Pass the combined flags as the second argument to
re.compile().

Program

import re

# Text to search
text = "Hi\nHI\nHi"

# Define a pattern with verbose mode for readability


pattern = r'''
Hi # match 'Hi'
'''

# Compile the pattern with re.IGNORECASE, re.DOTALL, and re.VERBOSE


comp = re.compile(pattern, re.IGNORECASE | re.DOTALL | re.VERBOSE)

# Find all matches in the text


matches = comp.findall(text)

# Print matches
print(matches) # Output: ['Hi', 'HI', 'Hi']

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

Explain search() and findall() method in regex. Also write the difference between search()
function and findall() function in regex
• The search() method scans the entire string and looks for matching object. It returns only
first matching object.
• It returns a match object (mo) if it finds match, otherwise it returns None.
• We have to use the group() method to get the matched object

import re

# Define the string


text = 'Call me at 123-456-7890 or 123-455-8890'

# Define the pattern


pattern = r'\d{3}-\d{3}-\d{4}'

# Compile the pattern


comp = re.compile(pattern)

# Search for the pattern using search()


mo = comp.search(text)
mo_g = mo.group() if mo else 'No match'

# Print the result


print(mo_g) # Output: 123-456-7890; only first occurrence

findall() Method:

• The findall() method scans through the entire string and finds all occurrences of the
pattern.
• It returns a list of all matched substrings.

import re

# Define the string


text = 'Call me at 123-456-7890 or 123-455-8890'

# Define the pattern


pattern = r'\d{3}-\d{3}-\d{4}'

# Compile the pattern


comp = re.compile(pattern)

# Find all matches using findall()


matches = comp.findall(text)

# Print the results


print(matches) # Output: ['123-456-7890', '123-455-8890'] ; returns all matched objects

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

File Handling
Explain the concept of file path. Also discuss absolute path and relative path.
File Path Definition:
• A file path is the specific location of a file or folder within a file system.
• It shows where a file is located starting from the root directory of the operating system.

• Components of a File Path: C:\Users\asweigart\Documents\ project.docx


• Root: The starting point of the file system hierarchy (C:\ ).
• Directories or folders: Intermediate folders, which will lead to
the destination file, separated by slashes ( \ ).
• File Name: The actual name of the file (project.docx)
• The extension (e.g., .docx) tells the file type.
• Example C:\Users\asweigart\Documents\ project.docx
Here C:\ is root directory
Users\asweigart\Documents are intermediate
folders
project.docx is file which is stored in folder
documents

There are 2 ways to specify the path


1. Absolute Path:
o It Specifies the complete location of a file or directory from the root of the file system.
o Example: C:\Users\asweigart\Documents\project.docx
o It is useful when you need to specify the exact location of a file regardless of the current
working directory.
o
2. Relative Path:
o It Specifies the location of a file or directory relative to the current folder or current working
directory.
o Example: Absolute path: C:\Users\asweigart\Documents\project.docx
o Relative path: ..Documents\project.docx.
o Helpful when referring to files or directories in relation to the current working directory.

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

Explain with suitable Python program segments:


i. os.path.basename()
ii. os.path.join().

os.path.join()
It is used to construct a path by joining one or more path components.
Example:
os.path.join('usr', 'bin', 'spam')
Output (windows): usr\bin\spam
Output (linux): usr/bin/spam
Useful for constructing file paths programmatically.

os.path.basename()
• It extracts the last component of a path, which typically represents the filename.
• Example: os.path.basename('C:\\Windows\\System32\\calc.exe')
• Output: calc.exe

Explain the concept of file handling. Also explain reading and writing process with suitable
example
File handling in Python refers to operations performed on files, such as reading or writing to them.
File operation takes place in the following order:
o Open a file
o Read or Write (Perform operation)
o Close the file

Opening Files with open () function


• Built-in open () function is used to open a file.
• The syntax of open () function:
f1= open (“filename”, “mode”)

Arguments:
filename: Name of the file to be opened. Pathname of the file is optional if file is in
current working directory.

f1: when we open file using open() function, python returns the file object. This file object
is stored in the variable f1, this is also called as handle. A file object represents a file in our
computer; it is simply another type of value in Python, much like the lists or dictionaries
This file object can then be used to perform operations (read/write) on the file.

mode: We have to specify what is purpose of opening a file (for reading or writing etc).

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

The various modes:

Example : consider notepad with file name :myfile.txt

Example: Opening a file:


# if file is in current folder (current directory)
f = open("myfile.txt”, “r”) #read mode

# Specifying full path (if file is in not current directory)


f = open(“D:\sujay\myfile.txt”, “r”)
Here myfile.txt is text file

Reading Files
• Consider the text file with name “myfile” written in notepad and stored in current directory.
Reading file using read() function.
read ()
This function is used to read the entire content of the file (if the argument is not mentioned)
read(n):
• It reads a specified number of characters (or bytes) from the file.
• For example, f.read(5) will read the first 5 characters from the file.
• If you do not specify a number inside the parentheses, read() will read the entire file.

# Open file in current folder


f1 = open(“myfile.txt”, “r”)
d1=f1.read()
print(d1)

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

Output:

• Here in this case, it reads the entire file at once and stores in the variable “d1”. So, this
method is suitable when the file size is very less.

Reading file using readlines() function.


readlines() : It reads the file line by line and stores each line as an element in a list.

# Open file in current folder


f1 = open(“myfile.txt”, “r”)
d1=f1.readlines()
print(d1)

Output:
Hello python \n',
Welcome python\n',
Hello India \n',
How are you \n',

Writing Files
• we can use write() method to write data into a file.
• write() can be used in 2 modes: mode “w” or mode “a”.
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
• If the file does not exist, then a new file (with the given name) will be created.
Example 1: To append
This method will append text to the end of the existing file. No overwriting in case of append
Open the file "myfile.txt" and append content to the file:
f = open("myfile.txt", "a")
f.write("Hello Virat \n")
f.write(“ Hello Dhoni”)
f.close()

#Now open and read the file after the appending:


f = open("myfile.txt", "r")
print(f.read())

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

• This data will be written at end of already existing content.


• Here first: “Hello Virat ” will be written into file, and since we have mentioned \n
character, the cursor will move to the next line.
• So, in the next line “Hello Dhoni” will be written.
• In case, if we had not mentioned \n character, then data will be written continuously

Example 2: To write
This method will over-write on the all-ready existing data
#Open the file "myfile.txt" and overwrite the content:
f = open(" myfile.txt", "w")
f.write("Hello Pandya")
f.close()
#open and read the file after the writing :
f = open("myfile.txt", "r")
print(f.read())

Develop a Python program to read and print the contents of a text file
# Program to read and print the contents of a text file named myfile.txt

# Open the file in read mode


f1 = open("myfile.txt", 'r')

# Read the entire content of the file


content = f1.read()

# Print the content


print(content)

# Ensure the file is closed


f1.close()

Develop python Program to read specific lines from a file


# Open the file in read mode
f1 = open("myfile.txt", "r")

# Read all lines into a list


lines = f1.readlines()

# Close the file


f1.close()

# Print the specific lines

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

for i in [0, 2]:


if i < len(lines):
print(lines[i]) # Print each specific line

Explain reading and saving python program variables using shelve module with suitable Python
program
• We can save variables of Python programs in binary shelf files using the shelve module.
• Binary shelf files are used to store data in a binary format.
• Example: After running a program and configuring some settings, we can save these
settings into a binary shelf file. Later, when we want to run the program again, we can load
the settings from the file instead of configuring them again.
• The shelve module allows us to implement Save and Open features in our programs.

Open shelf file and store data


import shelve

# Open a shelf file


shelf_file = shelve.open('mydata')

# Store a list of players in the shelf file, with key 'players'


players = ['Dhoni', 'Virat', 'Rohit']
shelf_file['players'] = players

# Close the shelf file


shelf_file.close()

To retrieve data
# Open the same shelf file
shelf_file = shelve.open('mydata')

# Retrieve the list of players using the key 'players'


retrieved_players = shelf_file['players']
print(retrieved_players) # Output: ['Dhoni', 'Virat', 'Rohit']

# Close the shelf file


shelf_file.close()

To convert to list
• Just like dictionaries, shelf values have keys() and values().
• This shelve method will return list-like values but not the true lists
• To get true lists, pass the returned values to the list() function.

# Open the shelf file again

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

shelf_file = shelve.open('mydata')

# Get keys and values from the shelf file, and convert them to lists
keys_list = list(shelf_file.keys())
values_list = list(shelf_file.values())

print(f"Keys: {keys_list}")
print(f"Values: {values_list}")

# Close the shelf file


shelf_file.close()

Explain Saving Variables with the pprint.pformat() Function

• The pprint.pformat() function from the pprint module is used to convert complex data
structures into a formatted string, which is easy to read.

import pprint

# Step 1: Define a data structure (list of dictionaries)


players = [{'name': 'Dhoni', 'desc': 'captain'}, {'name': 'Virat', 'desc': 'batsman'}]

# Step 2: Convert data to formatted string


formatted_players = pprint.pformat(players)

# Step 3: Write formatted data to a .py file


f1 = open('myPlayers.py', 'w')
f1.write(f'players = {formatted_players}\n')
f1.close()

# Step 4: Import and use the saved data


import myPlayers

# Step 5: Access the imported data


print(myPlayers.players) # [{'name': 'Dhoni', 'desc': 'captain'}, {'name': 'Virat', 'desc': 'batsman'}]
print(myPlayers.players[0]) # {'name': 'Dhoni', 'desc': 'captain'}
print(myPlayers.players[0]['name']) # 'Dhoni'

Prof. Sujay Gejji ECE, SGBIT, Belagavi


PYTHON PROGRAMMING

Develop a Python program to find the total size of all the files in the given directory.
To find the total size of all the files in the directory,
In this case, use os.path.getsize() and os.listdir() together

Prof. Sujay Gejji ECE, SGBIT, Belagavi

You might also like