Module 2 Advance Python
Files in Python, Directories, Building Modules, Packages, Text Processing, Regular
expression in python.
Files in Python
Opening, closing of a file, reading, writing from/to file - P2 -Pg 445
f=open('myfile.txt','w')
str=input('Enter text: ')
f.write(str)
f.close()
f=open('myfile.txt','r')
str1=f.read()
print(str)
f.close()
To store and read group of text to/ from file P3 and P4 – pg. 446 ,447
f=open('myfile.txt','w')
print('Enter text with @ at the end: ')
while str !='@':
str=input()
if (str!='@'):
f.write(str+"\n")
f.close()
f=open('myfile.txt','r')
str=f.read()
print(str)
f.close()
To append data to the file and then read the total content
f=open('myfile.txt','a+')
print('Enter text with @ at the end: ')
while str !='@':
str=input()
if (str!='@'):
f.write(str+"\n")
f.seek(0,0)
str=f.read()
print('The content of the file are: \n', str)
f.close()
Working with binary files P8 - pg. 451
f1= open('cat.jpg', 'rb')
f2=open('new.jpg', 'wb')
bytes=f1.read()
f2.write(bytes)
f1.close()
f2.close()
Opening a file with ‘with’ statement P9 -pg. 452
with open('myfile.txt','w') as f:
f.write('I am a learner \n')
f.write('Python is interesting \n')
with open('myfile.txt','r') as f:
for line in f:
print(line)
Pickle and unpickle in Python
import pickle
mylist = ['a', 'b', 'c', 'd']
with open('datafile.txt', 'wb') as fh:
pickle.dump(mylist, fh)
fh = open ("datafile.txt", "rb")
emp = pickle.load(fh)
print(emp)
fh.close()
P11 and 12 – pg 453, 454
Emp.py
class Employee:
def __init__(self,id, name, sal):
self.id=id
self.name=name
self.sal=sal
def display(self):
print('Employee ID is: ',self.id)
print('Employee name is: ', self.name)
print('Employee salary is:', self.sal)
main.py
import Emp, pickle
f=open('emp.dat', 'wb')
n=int(input('Enter total number of employee '))
for i in range(n):
id=int(input('Enter employee id: '))
name=input('Enter Employee name: ')
sal=float(input('Enter salary: '))
e=Emp.Employee(id, name, sal)
pickle.dump(e,f)
f.close()
f=open('emp.dat', 'rb')
print('Employee details: \n')
while True:
try:
obj=pickle.load(f)
obj.display()
except EOFError:
print(‘End of file reached’)
break
f.close()
Random accessing a binary file, seek, read, encode, decode P 14 and P15 pg 459
reclen=20
with open('cities.bin','wb') as f:
n=int(input('Enter number of cities'))
for i in range(n):
city=input('Enter the city name: ')
l=len(city)
city=city+ (reclen-l)*' '
city=city.encode()
f.write(city)
with open('cities.bin','rb') as f:
n=int(input('Enter the record number to be accessed: '))
f.seek(reclen*(n-1))
str=f.read(reclen)
print(str.decode())
Zip and Unzip file P21 and P22 – pg. 468, 469
from zipfile import*
f=ZipFile('test.zip', 'w', ZIP_DEFLATED)
f.write('file1.txt')
f.write('file2.txt')
f.write('file3.txt')
print('Zip file created ')
f.close()
z=ZipFile('test.zip','r')
z.extractall()
z.close()
Directories
Create directories and sub directory P27,P28 – pg. 472
import os
current =os.getcwd()
print('Current working directory is :', current)
os.mkdir('mysub')
goto=os.chdir('mysub')
current =os.getcwd()
print('Current working directory is :', current)
os.mkdir('mysubsub')
goto=os.chdir('mysubsub')
current =os.getcwd()
print('Current working directory is :', current)
rename os.rename(‘old’,’new’)
remove os.remove(‘subdir’)
Display the contents of current directory
import os
current =os.getcwd()
print('Current working directory is :', current)
os.mkdir('mysub')
goto=os.chdir('mysub')
current =os.getcwd()
print('Current working directory is :', current)
os.mkdir('mysubsub')
goto=os.chdir('mysubsub')
current =os.getcwd()
print('Current working directory is :', current)
for dirpath, dirnames, filenames in os.walk('/home'):
print('Current path :',dirpath)
print('Directories: ',dirnames)
print('Files: ', filenames)
#remove a directory
goto=os.chdir('/home/mysub')
current =os.getcwd()
print('Current working directory is :', current)
os.rmdir('mysubsub')
for dirpath, dirnames, filenames in os.walk('/home'):
print('Current path :',dirpath)
print('Directories: ',dirnames)
print('Files: ', filenames)
Module
Mymodule.py
def greeting(name):
print("Hello, " + name)
main.py
import mymodule
mymodule.greeting("VCET")
Regular expression
A regular expression is a string that contains special symbols and characters to find and extract the
information need from given data
Raw string
str='This is a \n normal string'
print(str)
str=r'This is a \n raw string'
print(str)
Search a word in string- search P1 -pg 480
import re
str=' man sun run'
result=re.search(r'm\w\w', str)
if result:
print(result.group())
search all the strings -P2 pg480
import re
str='bat mat sun mop run'
result=re.findall(r'm\w\w', str)
print(result)
match
import re
str='bat mat sun mop run'
result=re.match(r'm\w\w', str)
print(result)
import re
str='mat sun mop run'
result=re.match(r'm\w\w', str)
print(result)
split the string P5 -pg 482
+ is for all occurrences, W is to split at places where there are non alphanumeric characters, w is to
split at places where there are alphanumeric characters
import re
str='This; is a: "Core" Python \'s book'
result=re.split(r'\w+', str)
print(result)
result=re.split(r'\W+', str)
print(result)
Replace a string P6 -pg 482
import re
str='kumbhmela will be held at Ahmedabad'
result=re.sub(r'Ahmedabad', 'Allahabad',str)
print(result)
all words starting with a in a given string
import re
str=' an apple a day keeps the doctor away'
result=re.findall(r'a[\w]*',str)
print(result)
all words starting with a numeric digit
import re
str=' an apple 1 day keeps the doctor 21st away'
result=re.findall(r'\d[\w]*',str)
for word in result:
print(word)
all words having 4 characters length
import re
str=' one two three four five six seven '
result=re.findall(r'\b\w{4}\b',str)
for word in result:
print(word)
all words have 4 or more character length
import re
str=' one two three four five six seven '
result=re.findall(r'\b\w{4,}\b',str)
for word in result:
print(word)
import re
str=' one two three four five six seven '
result=re.findall(r'\b\w{3,4}\b',str)
for word in result:
print(word)
retrieve marks and names from a string
import re
str=' Rahul got 75 marks Meena got 98 marks'
marks=re.findall(r'\d{2}', str)
print(marks)
names=re.findall(r'[A-Z][a-z]*',str)
print(names)
Regular expression on file
read email-id from a file P24 pg 493
import re
f=open('mail.txt','r')
for line in f:
result=re.findall(r'\S+@\S+', line)
if result != None:
print(result)
f.close()
mail.txt
my personal id is [email protected]