DATA FILE HANDLING – TEXT FILE
1 Write a function to read total no. of This or this in a file
def Countthis():
f=open("[Link]",'r')
data=[Link]()
word=[Link]()#By default by space and return string into list
c=0
for i in word:
if i=='this' or i=='This':
c+=1
print(c)
Countthis()
2 #Write a function to read total no. of words of lenght>5 in a file
def Length():
f=open("[Link]",'r')
data=[Link]()
word=[Link]()#By default split by space
#and return string into list
c=0
for i in word:
if len(i)>5:
c+=1
print(c)
Length()
3 #read file line by line and display number of line and each line
def count():
f=open("[Link]",'r')
c1=0
while True:
d=[Link]()
if d=='':#no space
break
else:
c1+=1
print(d)
print(c1)
count()
4 #read file line by line and display number of line starting with E and T
def BeginE():
f=open("[Link]",'r')
c=0
d=[Link]()
for i in d:
if i[0]=='E' or i[0]=='T':
c+=1
print(i)
print(c)
BeginE()
#read file line by line and display line whose lenght is greater than 15
def count15():
f=open("[Link]",'r')
c=0
d=[Link]()
for i in d:
if len(i)>15:
c+=1
print(i)
print(c)
count15()
5 WAP to write file using writelines
with open("[Link]","w") as f:
d=["Computer Science","\nchemistry","\nPhysics"]
[Link](d)
6 WAP TO COPY CONTENT OF ONE FILE INTO ANOTHER FILE
#METHOD 1 - BY USING read() function and with(open) method
with open("[Link]","r") as f1:
d=[Link]()
print(d)
with open("[Link]","w") as f2:
[Link](d)
#METHOD 2 - BY USING read() function
f1=open("[Link]",'r')
d=[Link]()
f2=open("[Link]",'w')
[Link](d)
[Link]()
[Link]()
#METHOD 3 - BY USING readline() function
f1=open("[Link]","r")
f2=open("[Link]","w")
while True:
d=[Link]()
if d=='':
break
else:
[Link](d)
[Link]()
[Link]()
#METHOD 4 - BY USING readlines() function
f1=open("[Link]","r")
f2=open("[Link]","w")
d=[Link]()
for a in d:
[Link](a)
[Link]()
[Link]()