0% found this document useful (0 votes)
4 views6 pages

File Reading and Analysis in Python

Uploaded by

Burger Beast
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)
4 views6 pages

File Reading and Analysis in Python

Uploaded by

Burger Beast
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

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

f=open("[Link]",'r')
r=[Link]()
print(r)

#2. Wap to read a file and count the number of characters in the file.
ctr=0
f=open("[Link]", "r")
r=[Link]()
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("[Link]",'r')
x=[Link]()
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("[Link]",'r')
r=[Link]()
x=[Link]()
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("[Link]","r")
r=[Link]()
for i in r:
if [Link]():
uctr+=1
if [Link]():
lctr+=1
if [Link]():
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("[Link]",'r')
x=[Link]()
for i in [Link](" "):
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 [Link] lines starting with “A”.


actr=0
f=open("[Link]",'r')
x=[Link]()
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("[Link]",'r')
x=[Link]()
for i in x:
#[Link]()
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("[Link]",'r')
x=[Link]()
for i in [Link]():
print(i, end="*")
[Link]()

#10. Wap to read a file and copy the contents to another file.
f=open("[Link]",'r')
x=[Link]()
f1=open("[Link]",'w')
x1=[Link](x)
[Link]()
[Link]()
#11. Wap to find the no of vowels and consonants in a file.
vow=con=0
f=open("[Link]",'r')
x=[Link]()
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("[Link]",'r')
f1=open("[Link]",'w')
x=[Link]()
for i in x:
if [Link]('a') or [Link]('A'):
pass
else:
[Link](i)
[Link]()
[Link]()

#13. Wap to read a file and print along with line numbers
f=open("[Link]",'r')
x=[Link]()
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("[Link]",'r')
x=[Link]()
for i in [Link](" "):
if len(i)==4:
ctr1+=1
print("No of 4 letter words", ctr1)

You might also like