File handling practical code
File handling practical code
Consider a binary file “student.dat” that stores information about students using a
dictionary write a python function countrec() to display the number of students who get
minimum 75 percentage.
For example :
Code:
import pickle
def countrec():
c=0
F=open(“student.dat”,”rb”)
try:
while True:
d=pickle.load(f)
If d[“percentage”]>=75:
c=c+1
except EOFError:
f.close()
countrec()
For example:
[1001,”Ankit”,70000]
[2002,”Binay”,40000]
[3200,”Saroj”,35000]
Code:
import csv:
f=open(“emp.csv”,”r”,newline=’’)
freader=csv.reader(f)
c=0
for I in freader:
if i[2]>50000:
c=c+1
Consider a text file called alphabet.txt and then write a method countvoco() to read the
textfile and count the no of words starting with a vowel and an consonant in the text file
alphabet.txt
For example:
The greatest glory in living lies not in never falling but in rising every time we fall
Then the output
Code :
def countvoco():
f=open(“alphabet.txt”,”r”)
s=f.read()
l=s.split()
a=0
b=0
v=”AEIOUaeiou”
For i in l:
if i[0] in v:
a=a+1
else:
b=b+1
countvoco()
f.close()