Sunday, 23 August 2020

Text File Menu driven Program 1

PROGRAM:

 

  

  

  

   


OUTPUT:

     

   

      





























 #Text files Menu driven program

#Prepared by RKS


def word_as():

    file=open("D:\\test.txt","r")

    lines=file.readlines()

    for line in lines:

        words=line.split()

        for word in words:

            print(word+"#",end="")

        print("")

        file.close()


#main program

#word_as()


def countlines():

    file=open("D:\story.txt",'r')

    lines=file.readlines()

    count=0

    for word in lines:

        if word[0]=="A" or word[0]=="a":

            count=count+1

    print("total lines",count)

    file.close()


#main program

#countlines()


def displayword():

    file=open("D:\story.txt",'r')

    line=file.read()

    c=0

    word=line.split()

    for w in word:

        if len(w)<4:

            print(w)

            c=c+1

    print("number of words < 4 characters are: ",c)

    file.close()


#main program

#displayword()

#Prepared by RKS

    

while True:

    print("\n\nText files Menu driven program:")


    print("1. Read a text file line by line and")

    print(" display each word separated by a '#'\n")


    print("2. Program with function to count the ")

    print(" number of lines in a text file ‘ 'STORY.TXT’ ")

    print(" which is starting with an alphabet ‘A’ or ‘a’.\n")


    print("3. Program with method/function DISPLAYWORDS() ")

    print(" to read lines from a text file STORY.TXT, and ")

    print(" display those words, which are less than 4 characters\n ")

    

    print("4. Exit\n")


    ch=int(input("Enter your choice : "))

    if ch==1:

        word_as()

        

    elif ch==2:

       countlines() 


    elif ch==3:

       displayword()      


    elif ch==4:

        break


    else:

        print( " invalid choice ")


No comments:

Post a Comment