PART 1 - TEXT FILE
Example-
fout=open('[Link]','w')
[Link]("first line")
x=input("enter any key before flush")
[Link]()
x=input("enter any key after flush")
[Link]("second line")
[Link]()
Programs-
1 #write a program to count number of vowels in the file [Link]
fin=open("[Link]","r")
data=[Link]()
print(data)
c=0
for i in data:
if i in "aeiouAEIOU":
c=c+1
print("No. of vowels in file are :",c)
[Link]()
2 #write a program to count number of a in the file [Link]
fin=open("[Link]","r")
data=[Link]()
c=0
for i in data:
if i=="a":
c=c+1
print("No. of a in file are :",c)
[Link]()
3. # program to count number of upper letters in the file [Link]
#step 1 opening file
fin=open("[Link]","r")
#step 2 fetching data and process
data=[Link]()
print(data)
c=0
for i in data:
if [Link]():
c=c+1
print("total no. of a are:: ",c)
#step 3
[Link]()
4. # Program to count number of hello and there in the file [Link]
fin=open("[Link]","r")
data=[Link]()
print(data)
data1=[Link]()
print(data1)
c=0
for i in data1:
if [Link]()=="hello" or [Link]()=="there":
c=c+1
print("number of hello are::",c)
[Link]()
5. # Function to count words with length 5 characters
def count_5_word():
fin=open("[Link]","r")
data=[Link]()
words=[Link]()
print(words)
c=0
for i in words:
if len(i)==5:
c+=1
print("Total number of Words of Five characters are::",c)
[Link]()
count_5_word()
6. #write a program to count the lines starting with T in a file [Link]
fin=open("[Link]","r")
data=[Link]()
print(data)
c=0
for i in data:
if i[0]=='T':
c=c+1
print("Lines starting with T are::",c)
[Link]()
7. #write a program to count the lines with readline()
fin=open('[Link]' , "r")
c=0
line=" "
while line:
line=[Link]()
c=c+1
print(line)
[Link]()
8. #replace 'was' with 'is' and copying in other file
fin=open('[Link]','r')
fout=open('[Link]','w')
data=[Link]()
for i in data:
if 'was' in i:
[Link]([Link]('was','is'))
[Link]()
[Link]()
9 # function to write string in the text file using write()
fout=open('[Link]','w')
for i in range(0,4):
name=input('enter name')
[Link](name+'\n')
[Link]()
10 # writing string to text file using write()
fin=open('[Link]','w')
for i in range(0,3):
name=input('enter name')
marks=int(input('enter marks'))
rno=int(input('enter rno'))
s=str(rno)+" "+name+" "+str(marks)+"\n"
[Link](s)
[Link]()
11 # function to write name in the text file using writelines()
fout=open('[Link]','w')
l=[]
for i in range(0,4):
name=input('enter name')
[Link](name+'\n')
[Link](l)
[Link]()
12 # opening file using with statement
with open('[Link]','w') as fout:
for i in range(0,2):
name=input('enter name')
[Link](name+'\n')
SOME PRACTICE QUESTIONS-