Tuesday, 28 January 2020

Text file program menu driven

def rewr():
    #read a file line by line and print it
    f = open("cs.txt", 'w')
    line1 = "Welcome to 12th Class Python."
    f.write(line1)
    line2="\nPython is a general purpose and high level programming language"
    f.write(line2)
    line3 = "\nReadable and Maintainable Code"
    f.write(line3)
    line4="\nMultiple Paradigms"
    f.write(line4)
    line5 = "\nRobust Standard Library"
    f.write(line5)
    line6="\nSimplify Complex Software Development"
    f.write(line6)
    f.close()
    f = open("cs.txt", 'r')
    text = f.read()
    print(text)
    f.close()

def rem():
    #remove all the lines that contain the character 'a' in a file and write it to another file
    fin=open("D:\\book.txt","r")
    fout=open("D:\\story.txt","a")
    s=fin.readlines()
    for j in s:
        if 'a' in j:
            fout.write(j)
    fout.close()
    fin.close()

def ch4():
    #display those words, which are less than 4 characters
    file=open("D:\story.txt")
    line=file.read()
    c=0
    word=line.split()
    for w in word:
        if len(w)<4:
            print(w)
        file.close

#main program
n=int(input("enter the choice 1-read/write, 2-print a and 3-less 4"))
if n==1:
      rewr()
elif n==2:
      rem()
else:
      ch4()

No comments:

Post a Comment