Data File Handling Abcd
Data File Handling Abcd
COMPUTER SCIENCE
x=input("enter string")
f = open("text.txt","w")
f.write(x)
f.close()
f = open("text.txt","r")
y = f.read()
f.close()
counts=0
count_lo=0
count_up=0
print(y)
print(type(y))
for i in y:
if i == " ":
counts+=1
elif str(i).islower():
count_lo+=1
elif str(i).isupper():
count_up+=1
x=input("enter string")
f = open("text.txt","w")
f.write(x)
f.close()
f = open("text.txt","r")
y = f.read()
f.close()
counts=0
for i in y:
if i == " ":
counts+=1
twords = counts+1
spl=y.split()
countis=0
counta=0
counts_=0
countthe=0
for i in spl:
if i.lower() == "is":
countis+=1
elif i[0]=="a":
counta+=1
elif i[0]=="s":
counts_+=1
elif i[0:4]=="the":
countthe+=1
elif len(i)>=4:
print(i)
vowel=0
consonant=0
for i in spl:
for k in i:
if k.lower() in "aeiou":
vowel+=1
else:
consonant+=1
print(i,"vowel = ", vowel, " consonant = ", consonant)
17. Count number of lines.
18. Count number of lines starting with ‘t’.
19. Count number of lines ending with ‘y’.
20. Count the number of lines starting with ‘they’.
21. Count the number of lines ending with ‘free’.
22. Write code to print just the last line.
23.Write code to print just the last line.
f = open("txt2.txt","w")
f.write("hello\n")
f.write("why\n")
f.write("python\n")
f.write("tiger\n")
f.write("py\n")
f.write("theybe\n")
f.close()
f=open("txt2.txt","r")
length = len(f.readlines())
f.close()
f=open("txt2.txt","r")
print(f.readlines())
f.close()
f=open("txt2.txt","r")
count=0
count_t=0
count_y=0
count_they=0
for line in f.readlines():
if line[-3]=="y":
count_y+=1
print(line)
elif line[0]=="t":
count_t+=1
elif line[0:5]=="they":
count_they+=1
f.close()
print("no of lines = ",length)
print("no of line with t = ", count_t)
print("no of line with y = ", count_y)
print("no of line with they = ", count_they)
23. Write a program to take the details (title and price) of book
from the user and write the record in text file.
d1={}
ch= True
while ch:
title=input("enter book title")
price=int(input("enter price"))
d1[title]=price
ch2=input("enter y or n")
if ch2.lower()=="n":
ch=False
f=open("txt3.txt","w")
for i,j in d1.items():
f.write(i + " ")
f.write(str(j)+"\n")
f.close()
f=open("txt3.txt","r")
print(f.read())
f.close()
24. Write a program to take the details (title and price) of book
from the user and write the record in the end of the text file.
d1={}
ch= True
while ch:
title=input("enter book title")
price=int(input("enter price"))
d1[title]=price
ch2=input("enter y or n")
if ch2.lower()=="n":
ch=False
f=open("txt3.txt","a")
for i,j in d1.items():
f.write(i + " ")
f.write(str(j)+"\n")
f.close()
f=open("txt3.txt","r")
print(f.read())
f.close()