5 6055295234659585269 (1) - 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

#File Operations on TEXT File

#File copy except the letter 'a' using line by line


f1=open('note.txt')
f2=open('B.txt','w')
d=f1.readline()
while d!='':
if 'a' not in d:
f2.write(d)
d=f1.readline()
#print('*',end='')
f2.close()
f1.close()
print('File copied')

#convert lowercase to uppercase and vice-versa


f1=open('note.txt')
f2=open('B.txt','w')
d=f1.read()
#d=input('Any String :')
for i in d:
if i.isupper():
x=i.lower()
else:
x=i.upper()
f2.write(x)
#print(x,end='')
f2.close()
f1.close()
print('File copied')

#Program to read line from file and write it to another line


#Except for those line which contains letter “a”
f1 = open("FILE.TXT")
f2 = open("FILE1.TXT","w")
x=f1.readlines()
for line in x:
if 'a' not in line:
f2.write(line)
print("## File Copied Successfully! ##")
f1.close()
f2.close()
#Count Vowels , consonant, special char , digits etc.,
f1=open('note.txt')
f2=open('B.txt','w')
d=f1.read() #d=input('Enter any String :')
Vow=['a','e','i','o','u','A','E','I','O','U']
V=C=U=L=O=0
for i in d:
if i.isalpha():
if i.isupper():
U+=1
else:
L+=1
if i in Vow:
V+=1
else:
C+=1
else:
O+=1
print("Number of Vowels : ",V)
print("Number of Consonants : ",C)
print("Number of Uppercases : ",U)
print("Number of Lowercases : ",L)
print("Number of Others : ",O)

f2.close()
f1.close()
print('File copied')

#Program to read content of file line by line


#and display each word separated by '#'
f = open("file.txt")
line=f.readline()
for x in line:
words = line.split()
for w in words:
print(w+'#',end='')
print()
line=f.readline()
f.close()
#program to copy file1.txt into file2.txt
fin=open("file1.txt")
fout=open("file2.txt","w")
data=fin.read( )
fout.write(data)
fin.close( )
fout.close( )

#displays the number of lines starting with ‘H’ in the file

Ans:
def countH():
f = open ("Para.txt", "r")
lines =0
l =f.readlines()
for i in l:
if i[0]=='H':
lines+=1
print ("no. of lines is", lines)
f.close( )

#Except a particular letter 'H' or 'h'


f1=open('note.txt')
f2=open('B.txt','w')
d=f1.read()
for i in d:
if i !='H' and i!='h':
f2.write(i)
f2.close()
f1.close()
print('File copied')

#Counting the words 'the'


f1=open('note.txt')
f2=open('B.txt','w')
d=f1.readline()
cnt=0
while d!='':
if 'the' in d:
x=d.count('the')
cnt+=x
print('The word appears ',x,' times in ',d)
else:
f2.write(d)
d=f1.readline()
print(cnt)
f2.close()
f1.close()
print('File copied')

#Capitalize the First letter of each line


f1=open('note.txt')
f2=open('B.txt','w')
d=f1.readline()
while d!='':
if d[0].isalpha():
d=d[0].upper()+d[1:]
f2.write(d)
d=f1.readline()
f2.close()
f1.close()
print('File copied')

#Count number of lines/characters/words


f1=open('note.txt')
def dispwords():
d=f1.readline()
L=C=W=0
while d!='':
L+=1
C+=len(d)
Words=d.split()
for i in Words:
W+=1
d=f1.readline()
print('Total number of Lines : ',L)
print('Total number of Characters : ',C)
print('Total number of Words : ',W)
dispwords()
f1.close()

#Display the words started with uppercase letters


f1=open('note.txt')
def dispwords():
d=f1.readline()
while d!='':
Words=d.split()
for word in Words:
if word[0].isupper():
print(word,end=' ')
d=f1.readline()
dispwords()
f1.close()

#Display the file contents and number of digits


f1=open('note.txt')
def disp():
d=f1.read()
digi=0
for i in d:
if i.isdigit():
digi+=1
print('The content of the file is \n',d)
print('Number of digits ',digi)
disp()
f1.close()

#Display number of lines and started with the letter A/B/C


f1=open('note.txt')
def disp():
d=f1.readline()
L=C=0
while d!='':
L+=1
if d[0].isalpha():
if d[0].isupper():
if d[0]=='A' or d[0]=='B' or d[0]=='C':
C+=1
d=f1.readline()
print('Total number of Lines : ',L)
print('Total number of words started with A/B/C is : ',C)
disp()
f1.close()

#Display the words which are less than 4 characters


f1=open('note.txt')
def disp():
d=f1.readline()
while d!='':
Words=d.split()
for word in Words:
if len(word)<4:
print(word,end=' ')
d=f1.readline()
disp()
f1.close()

#Display the words which are end with 'n'


f1=open('note.txt')
def disp():
d=f1.readline()
cnt=0
while d!='':
Words=d.split()
for word in Words:
if word[-1]=='n':
cnt+=1
print(word,end=' ')
d=f1.readline()
print("\nTotal number of words end with 'n' is ",cnt)
disp()
f1.close()

#Display the words which are end with 'n'


f1=open('note.txt')
def disp():
d=f1.readlines()
cnt=0
for i in d:
Words=i.split()
for word in Words:
if word[-1]=='n':
cnt+=1
print(word,end=' ')
print("\nTotal number of words end with 'n' is ",cnt)
disp()
f1.close()

#File Operations on BINARY File

import pickle
Sdet=[]
def AddRec(): #1-Adding a record
f1=open('STUD.DAT','wb')
sno=int(input('Enter Student Number '))
sname=input('Enter Student Name ')
smark=int(input('Enter Mark '))
Sdet.append([sno,sname,smark])
pickle.dump(Sdet,f1) #pickle.dump(value,filehandle)
print("A record inserted")
f1.close()

def SearchRec(): #2-Searching a record


Sdet=[]
f2=open('STUD.DAT','rb')
while True:
try:
Sdet=pickle.load(f2)
except EOFError:
break
sno=int(input('Enter serial number to Search '))
Found=0
for s in Sdet:
if s[0]==sno:
print('Name : ',s[1],'Mark : ',s[2])
Found=1
break
if Found==0:
print('No record found...')
f2.close()

def UpdateRec(): #3-Updating a record


Sdet=[]
f1=open('STUD.DAT','rb')
while True:
try:
Sdet=pickle.load(f1) #Variable=pickle.load(FileHandle)
except EOFError:
break
f1.close()
f2=open('STUD.DAT','wb')
sno=int(input('Enter serial number to Update '))
Found=0
for s in Sdet:
if s[0]==sno:
sname=input('Enter a new name ')
print('Before updation ')
print(s)
print('After updation ')
s[1]=sname
print(s)
Found=1
break
pickle.dump(Sdet,f2)
if Found==0:
print('No record found...')
f2.close()

def DelRec(): #4-Deleting a record


Sdet=[]
f1=open('STUD.DAT','rb')
while True:
try:
Sdet=pickle.load(f1)
except EOFError:
break
f1.close()
f2=open('STUD.DAT','wb')
sno=int(input('Enter serial number to delete '))
Found=0
cnt=0
for s in Sdet:
if s[0]==sno:
print('Before deletion ')
print(Sdet)
print('After deletion ')
Sdet.pop(cnt)
print(Sdet)
Found=1
break
cnt+=1
pickle.dump(Sdet,f2)
if Found==0:
print('No record found...')
f2.close()

def ShowRec(): #5-Displaying records


Sdet=[]
f2=open('STUD.DAT','rb')
while True:
try:
Sdet=pickle.load(f2)
except EOFError:
break
print(Sdet)
f2.close()

while True:
opt=int(input('1-Add 2-Search 3-Update 4-Delete 5-Show 6-Exit'))
if opt==1:
AddRec()
elif opt==2:
SearchRec()
elif opt==3:
UpdateRec()
elif opt==4:
DelRec()
elif opt==5:
ShowRec()
elif opt==6:
break
else:
print('Invalid input ')
print('File operation is done...')

#File Operations on CSV File

import csv

def AddRec(): #1-Adding a record


with open('MYCSVFILE.CSV','a') as f1:
W=csv.writer(f1,delimiter=',')
sno=int(input('Enter Student Number '))
sname=input('Enter Student Name ')
smark=int(input('Enter Mark '))
W.writerow((sno,sname,smark))
print('Record inserted...')

def SearchRec(): #2-Searching a record


f1=open('MYCSVFILE.CSV','r')
R=csv.reader(f1,delimiter=',')
sno=int(input('Enter serial number to Search '))
Found=0
for row in R:
if len(row)!=0:
if int(row[0])==sno:
print('Name : ',row[1],'Mark : ',row[2])
Found=1
break
if Found==0:
print('No record found...')
f1.close()

def ShowRec(): #3-Displaying records


f1=open('MYCSVFILE.CSV','r')
R=csv.reader(f1,delimiter=',')
Found=0
for row in R:
if len(row)!=0:
print(row)
Found=1
if Found==0:
print('No record found...')
f1.close()

while True:
opt=int(input('1-Add 2-Search 3-Show 4-Exit'))
if opt==1:
AddRec()
elif opt==2:
SearchRec()
elif opt==3:
ShowRec()
elif opt==4:
break
else:
print('Invalid input ')
print('File operation is done...')

#seek( ) and tell( )


f=open("D:\File handling programs\\sample.txt",'r')
print("Before reading,File Pointer position:",f.tell())
s=f.read()
print("After reading,File Pointer position:",f.tell())
f.seek(0) #Brings the file pointer to the 0th Byte
print("From the beginning of the file again:",f.tell())
s=f.read(4)
print("First 4 bytes are:",s)
print("File pointer position:",f.tell())
s=f.read(3)
print("Next 4 bytes are:",s)
print("File pointer position:",f.tell())
f.close()

You might also like