Module 2 Files in Python - Programs
Module 2 Files in Python - Programs
Files in Python
Opening, closing of a file, reading, writing from/to file - P2 -Pg 445
f=open('myfile.txt','w')
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')
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()
f2=open('new.jpg', 'wb')
bytes=f1.read()
f2.write(bytes)
f1.close()
f2.close()
with open('myfile.txt','w') as f:
with open('myfile.txt','r') as f:
for line in f:
print(line)
import pickle
mylist = ['a', 'b', 'c', 'd']
pickle.dump(mylist, fh)
emp = pickle.load(fh)
print(emp)
fh.close()
Emp.py
class Employee:
self.id=id
self.name=name
self.sal=sal
def display(self):
main.py
f=open('emp.dat', 'wb')
n=int(input('Enter total number of employee '))
for i in range(n):
pickle.dump(e,f)
f.close()
f=open('emp.dat', 'rb')
while True:
try:
obj=pickle.load(f)
obj.display()
except EOFError:
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:
for i in range(n):
l=len(city)
city=city.encode()
f.write(city)
with open('cities.bin','rb') as f:
f.seek(reclen*(n-1))
str=f.read(reclen)
print(str.decode())
Zip and Unzip file P21 and P22 – pg. 468, 469
f.write('file1.txt')
f.write('file2.txt')
f.write('file3.txt')
f.close()
z=ZipFile('test.zip','r')
z.extractall()
z.close()
Directories
import os
current =os.getcwd()
os.mkdir('mysub')
goto=os.chdir('mysub')
current =os.getcwd()
os.mkdir('mysubsub')
goto=os.chdir('mysubsub')
current =os.getcwd()
rename os.rename(‘old’,’new’)
remove os.remove(‘subdir’)
import os
current =os.getcwd()
os.mkdir('mysub')
goto=os.chdir('mysub')
current =os.getcwd()
os.mkdir('mysubsub')
goto=os.chdir('mysubsub')
current =os.getcwd()
print('Directories: ',dirnames)
#remove a directory
goto=os.chdir('/home/mysub')
current =os.getcwd()
os.rmdir('mysubsub')
print('Directories: ',dirnames)
print('Files: ', filenames)
Module
Mymodule.py
def greeting(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
print(str)
print(str)
import re
result=re.search(r'm\w\w', str)
if result:
print(result.group())
result=re.findall(r'm\w\w', str)
print(result)
match
import re
result=re.match(r'm\w\w', str)
print(result)
import re
result=re.match(r'm\w\w', str)
print(result)
+ 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
result=re.split(r'\w+', str)
print(result)
result=re.split(r'\W+', str)
print(result)
import re
result=re.sub(r'Ahmedabad', 'Allahabad',str)
print(result)
import re
result=re.findall(r'a[\w]*',str)
print(result)
all words starting with a numeric digit
import re
result=re.findall(r'\d[\w]*',str)
print(word)
import re
result=re.findall(r'\b\w{4}\b',str)
print(word)
import re
result=re.findall(r'\b\w{4,}\b',str)
print(word)
import re
result=re.findall(r'\b\w{3,4}\b',str)
print(word)
import re
print(marks)
names=re.findall(r'[A-Z][a-z]*',str)
print(names)
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]