0% found this document useful (0 votes)
5 views

Text Files Questions

Uploaded by

Burger Beast
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Text Files Questions

Uploaded by

Burger Beast
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#1. Wap to read a file line by line.

f=open("textfile1.txt",'r')
r=f.readlines()
print(r)

#2. Wap to read a file and count the number of characters in the file.
ctr=0
f=open("textfile1.txt", "r")
r=f.read()
for i in r:
ctr+=1
print("The no of characters is : ", ctr)

#3. Wap to read a file and count the no of a's in the file.
actr=0
f=open("textfile1.txt",'r')
x=f.read()
for i in x:
if i in 'Aa':
actr+=1
print("The no of a's : ", actr)
#4. Wap to read a file and count the no of words in the file.

wctr=0
f=open("textfile1.txt",'r')
r=f.read()
x=r.split()
for i in x:
wctr+=1
print("No of words: ", wctr)

#5. Wap to read a file and count the number of lower, upper,
punctuations and numbers.
uctr=lctr=pctr=dctr=0
f=open("textfile2.txt","r")
r=f.read()
for i in r:
if i.isupper():
uctr+=1
if i.islower():
lctr+=1
if i.isdigit():
dctr+=1
if i in """,.':""":
pctr+=1
print(uctr, lctr, dctr, pctr)
#6. Wap to count the number of ‘the’ and ‘my’ in a file
tctr=0
mctr=0
f=open("textfile1.txt",'r')
x=f.read()
for i in x.split(" "):
if i=='the':
tctr=tctr+1
if i=='my':
mctr=mctr+1
print("No of the: ",tctr)
print("No of my: ",mctr)

#7. Wap to count the no.of lines starting with “A”.


actr=0
f=open("textfile1.txt",'r')
x=f.readlines()
for i in x:
if i[0]=='a' or i[0]=='A':
actr+=1
print("No of lines with A: ",actr)
#8. Wap to count the no of lines ending with ‘t’.
ctr=0
f=open("textfile1.txt",'r')
x=f.readlines()
for i in x:
#i.strip()
if i[-2]=='t' or i[-2]=='T':
ctr+=1
print("No of lines with t: ",ctr)

#9. Wap to print the contents of a file with * as delimiter for each
word
f=open("textfile1.txt",'r')
x=f.read()
for i in x.split():
print(i, end="*")
f.close()

#10. Wap to read a file and copy the contents to another file.
f=open("textfile1.txt",'r')
x=f.readlines()
f1=open("textfile2.txt",'w')
x1=f1.writelines(x)
f.close()
f1.close()
#11. Wap to find the no of vowels and consonants in a file.
vow=con=0
f=open("textfile1.txt",'r')
x=f.read()
for i in x:
if i in 'aeiouAEIOU':
vow=vow+1
elif i in " .\n":
pass
else:
con+=1
print(vow, con)

#12. Wap to copy the contents of a file to another file without the
lines starting with ‘a’.
f=open("textfile1.txt",'r')
f1=open("textfile2.txt",'w')
x=f.readlines()
for i in x:
if i.startswith('a') or i.startswith('A'):
pass
else:
f1.writelines(i)
f1.close()
f.close()

#13. Wap to read a file and print along with line numbers
f=open("textfile1.txt",'r')
x=f.readlines()
for i in range(0,len(x)):
print(i+1,".", x[i])

#14. Wap to count the no of words which has only 4 characters in a


file.
ctr1=0
f=open("textfile1.txt",'r')
x=f.read()
for i in x.split(" "):
if len(i)==4:
ctr1+=1
print("No of 4 letter words", ctr1)

You might also like