Wednesday, 9 April 2025

GRADE 12 : PRACTICAL 6 : TEXT FILES

 ''' PRACTICAL 6

WAP to create functions that work on 'essay.txt' :

1. Accept data and store in the file

2. Display content of the file

3. Display Line Count, Word Count, Character Count

4. Search words that start with vowels

5. Change all the occurences of 'the' to THE

6. Exit

'''


def menu():

    while True:

        ch=int(input("""

MENU:

1. Accept Data

2. Display Data

3. Display Statistics

4. Search words that start with vowels

5. Change all occurences of 'the' to 'THE'.

6. Exit

Your choice : 

"""))

        if ch ==1:

            accept()

        elif ch ==2:

            display()

        elif ch ==3:

            count()

        elif ch ==4:

            search()

        elif ch ==5:

            change()

        else:

            break


def accept():

    n=int(input("Enter no. of lines"))

    with open('essay.txt','w') as fobj:

        print(f'Please enter {n} lines ')

        for i in range(n):

            line=input()

            fobj.write(line+'\n')

    

def display():

    try:

        fobj=open('essay.txt','r')

        content=fobj.read()

        print(content)

        fobj.close()

    except Exception as e:

        print("Error : ",e)

def count():

    try:

        fobj=open('essay.txt','r')

        text =fobj.read()

        cc=len(text)

        wc=len(text.split())

        lc=len(text.splitlines())

        print("Character count : ",cc)

        print("Word count : ",wc)

        print("Line count : ",lc)

        fobj.close()

    except Exception as e:

        print("Error : ",e)

        

def search():

    try:

        with open("essay.txt",'r') as fobj:

            text=fobj.read()

            words=text.split()

            for i in words:

                if i[0] in 'aeiouAEIOU':

                    print(i)

    except Exception as e:

        print("Error : ",e)

        

def change():

    with open('essay.txt','r') as fobj:

        text=fobj.read()

    text=text.replace('the','THE')

    with open('essay.txt','w') as fobj:

        fobj.write(text)

    


menu()



No comments:

Post a Comment