0% found this document useful (0 votes)
7 views5 pages

File Handling Class12-Hots

The document contains a series of Python programming exercises focused on file handling, including reading from and writing to text and CSV files. Tasks include categorizing characters, counting vowels, filtering lines, and managing employee and furniture records. It also discusses the advantages of CSV files over binary files and provides sample code for each exercise.

Uploaded by

sanjay2912009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

File Handling Class12-Hots

The document contains a series of Python programming exercises focused on file handling, including reading from and writing to text and CSV files. Tasks include categorizing characters, counting vowels, filtering lines, and managing employee and furniture records. It also discusses the advantages of CSV files over binary files and provides sample code for each exercise.

Uploaded by

sanjay2912009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

VELAMMAL VIDHYASHRAM SURAPET

CHENNAI - 66
Class – 12
Worksheet –File handling Tilak Wing
1.
'''Write a program that reads character from the keyboard one by one. All lower case characters get
store inside the file LOWER, all upper case characters get stored inside the file UPPER and all other
characters get stored inside OTHERS.
'''
f=open("hello.txt")
f1=open("lower.txt","a")
f2=open("upper.txt","a")
f3=open("others.txt","a")

r=f.read()

for i in r:
if(i>='a' and i<='z'):
f1.write(i)
elif(i>='A' and i<='Z'):
f2.write(i)
else:
f3.write(i)

f.close()
f1.close()
f2.close()
f3.close()

2.
'''Write a function VowelCount() in Python, which should read each character of a text file .TXT, should
count and display the occurrence of alphabets vowels.'''

def VowelCount():
a=e=i=o=u=0
f= open('hello.TXT', 'r')
d=f.read()
for letter in d:
if letter.upper()=='A':
a+=1
elif letter.upper()=='E':
e+=1
elif letter.upper()=='I':
i+=1
elif letter.upper()=='O':
o+=1
elif letter.upper()=='U':
u+=1
print("A or a:", a)
print("E or e:", e)
print("I or i:", i)
print("O or o:", o)
print("U or u:", u)
VowelCount()

3. '''Write a method in python to read lines from a text file hello.TXT and display those lines which
start with the alphabets S.'''

def countp():
f=open("hello.txt","r")
lines=0
l=f.readlines()
for i in l:
if i[0]=='S':
lines+=1
print("No of lines are:",lines)

4. ''' Write a program to accept 10 numbers from the user. If the number is even than write that number
in even.txt, otherwise, write in file "odd.txt'''

f=open("even.txt","a")
f1=open("odd.txt","a")
for i in range(10):
n=int(input("enter number"))
if(i%2==0):
f.write(str(n))
f.write("\n")
else:
f1.write(str(n))
f1.write("\n")
f.close()
f1.close()

5. '''Write a method in python to read lines from a text file hello.TXT and display those lines which
start with the alphabets S.'''

def countS():
f=open("hello.txt","r")
lines=0
l=f.readlines()
for i in l:
if i[0]=='S':
lines+=1
print("No of lines are:",lines)

6. A file sports.dat contains information in following format [event, participant].


Write a program that would read the contents from file and copy only those records from sports.dat
where the event name is “Athletics” in new file named Athletics.dat
def countrec():
num=0
fobj=open("data.dat","rb")
try:
print("Emp id\tEmp Name\tEmp Sal")
while True:
rec=pickle.load(fobj)
if rec[2]>20000:
print(rec[0],"\t\t",rec[1],"\t\t",rec[2])
except:
fobj.close()
countrec()
7. A file sports.dat contains information in following format [event, participant].
Write a program that would read the contents from file and copy only those records from
sports.dat where the event name is “Athletics” in new file named Athletics.dat
import pickle
F1 = open ("sports.dat", "rb")
F2 = open ("athletics.dat", "wb")
sum = 0
while True:
try:
l = pickle.load(F1)
if (l[0].lower() == "athletics"):
print (l)
pickle.dump(l,F2)
except EOFError:
break
F1.close()
F2.close()

8.
What is the advantage of using a csv file for permanent storage? Write a Program in Python that defines
and calls the following user defined functions:
(i) ADD() – To accept and add data of an employee to a CSV file ‘record.csv’. Each record consists of a list
with field elements as empid, name and mobile to store employee id, employee name and employee
salary respectively.
(ii) COUNTR() – To count the number of records present in the CSV file named ‘record.csv’.

Advantage of a csv file:


It is human readable – can be opened in Excel and Notepad applications
It is just like text file
Program:
import csv
def ADD():
fout=open("record.csv","a",newline="\n")
wr=csv.writer(fout)
empid=int(input("Enter Employee id :: "))
name=input("Enter name :: ")
mobile=int(input("Enter mobile number :: "))
lst=[empid,name,mobile]
wr.writerow(lst)
fout.close()

def COUNTR():
fin=open("record.csv","r",newline="\n")
data=csv.reader(fin)
d=list(data)
print(len(d))
fin.close()
ADD()
COUNTR()

9.
Give any one point of difference between a binary file and a csv file.
Write a Program in Python that defines and calls the following user defined functions:
a) add() – To accept and add data of an employee to a CSV file ‘furdata.csv’. Each record
consists of a list with field elements as fid, fname and fprice to store furniture id, furniture
name and furniture price respectively.
b) search()- To display the records of the furniture whose price is more than 10000.
Ans:
Difference between binary file and csv file: (Any one difference may be
given)
Binary file:
 Extension is .dat
 Not human readable
 Stores data in the form of 0s and 1s
CSV file
 Extension is .csv
 Human readable
 Stores data like a text file
Program:
import csv
def add():
fout=open("furdata.csv","a",newline='\n')
wr=csv.writer(fout)
fid=int(input("Enter Furniture Id :: "))
fname=input("Enter Furniture name :: ")
fprice=int(input("Enter price :: "))
FD=[fid,fname,fprice]
wr.writerow(FD)
fout.close()
def search():
fin=open("furdata.csv","r",newline='\n')
data=csv.reader(fin)
found=False
print("The Details are")
for i in data:
if int(i[2])>10000:
found=True
print(i[0],i[1],i[2])
if found==False:
print("Record not found")
fin.close()
add()
print("Now displaying")
search()

10. Write a python program to create a csv file dvd.csv and write 10 records in it Dvd id,dvd
name,qty,price. Display those dvd details whose dvd price is more than 25.

import csv
f=open("dvd.csv","a")
cw=csv.writer(f)
ch="Y"
while ch=="Y":
l=[]
pi=int(input("enter dvd id "))
pnm=input("enter dvd name ")
sp=int(input("enter qty "))
p=int(input("enter price(in rupees) "))
l.append(pi)
l.append(pnm)
l.append(sp)
l.append(p)
cw.writerow(l)
ch=input("do you want to enter more rec(Y/N): ").upper()
if ch=="Y":
continue
else:
break
f.close()
f=open("dvd.csv","r")
cw=list(csv.reader(f))
for i in cw:
if l[3]>25:

print(i)
f.close()

You might also like