Q1.
Write a program to write those lines which starts with the
character 'p' from one text file to another text file.
sol.
file1=open('[Link]','r')
lines=[Link]()
file2=open('[Link]','w')
for line in lines:
if line[0]=='p' or line[0]=='P':
[Link](line)
[Link]()
[Link]()
Q2. Write a program to count and display the no of lines starting with
‘S’ present in a text file “[Link]”.
sol.
myfile=open('[Link]','r')
lines=[Link]()
count=0
for line in lines:
if line[0]=='S' or line[0]=='s':
count+=1
[Link]()
print("Number of lines starting with 'S' are",count)
Q3. Write a method/function DISPLAYVOWEL() in python to read lines
from a text file [Link], and display those words, which start with a
vowel(small or capital).
def DISPLAYVOWEL():
vowels=['a','e','i','o','u','A','E','I','O','U']
myfile=open('[Link]','r')
words=[Link]().split()
for word in words:
if word[0] in vowels :
print(word[0])
[Link]()
Q4. Write a function in python to count the number of lines in a text file
‘[Link]’ which are ending with an alphabet ‘e’ .
def countende():
myfile=open('[Link]','r')
lines=[Link]()
count=0
for line in lines:
if line[-1]=='\n':
if line[-2]=='e' or line[-2]=='E':
count+=1
elif line[-1]=='e' or line[-1]=='E':
count+=1
[Link]()
print("Number of lines ending with 'e' are",count)
Q5. Write a function remove_lowercase( ) that accepts two filenames,
and copies all lines that do not start with a lowercase letter from the
first file into the second.
def remove_lowercase(file1,file2):
myfile1=open(file1,'r')
myfile2=open(file2,'w')
lines=[Link]()
for line in lines:
if line[0].isupper():
[Link](line)
[Link]()
[Link]()