Check sibling questions

Write a method in Python to read lines from a text file DIARY.TXT, and display those lines, which are starting with an alphabet ‘P’.

 

Answer:

def count():

f=open("DIARY.txt",'r')

l=f.readlines()

for line in l:

if line[0]=='P':

print(line)

f.close()

Question 1-Write a method in Python - Teachoo.JPG

Introducing your new favourite teacher - Teachoo Black, at only ₹83 per month


Transcript

Question 1 Write a method in Python to read lines from a text file DIARY.TXT, and display those lines, which are starting with an alphabet ‘P’. declaring function count( ) reading all the lines in the text file and assigning it to ‘l’ Checking if the first letter of a line is ‘P’ Closing the file opening the text file in read mode and assigning it to file_object ‘f’ Iterating through each line in ‘l’ and assigning it to ‘line’ Prints the line which starts with ‘P’ Invoking function count( ) Output

CA Maninder Singh's photo - Expert in Practical Accounts, Taxation and Efiling

Made by

CA Maninder Singh

CA Maninder Singh is a Chartered Accountant for the past 12 years. He also provides Accounts Tax GST Training in Delhi, Kerala and online.