Read,write and append program in python

Example program for reading, write and append:

#Write functions to create a text file called student.txt to store few student details in a list of values

#like enrollment number, name, gender, standard, and section. Also, write functions for the following

#A. Append

#S. Search

#D.Display

#E.Exit

 Program:

  1. def CREATE():

        f=open("D:\\KURSHITHA\\Notepad\\student.det","w")

        ch=1

        while ch:

            roll=int(input("Enter Roll Number:"))

            name=input("Enter name:")

            gen=input("Enter gender:")

            std=int(input("Enter std:"))

            sec=input("Enter Section:")

            rec=str(roll)+" "+name+" "+gen+" "+str(std)+" "+sec

            f.write(rec)

            f.write('\n')

            ch=int(input("Any more records (1 or 0 ):"))

        f.close()

     

    def APPEND():

        f=open("D:\\KURSHITHA\\Notepad\\student.det","a")

        ch=1

        while ch:

            roll=int(input("Enter Roll Number:"))

            name=input("Enter name:")

            gen=input("Enter gender:")

            std=int(input("Enter std:"))

            sec=input("Enter Section:")

            rec=str(roll)+" "+name+" "+gen+" "+str(std)+" "+sec

            f.write(rec)

            f.write('\n')

            ch=int(input("Any more records (1 or 0 ):"))

        f.close()

     

    def PRINT():

        f=open("D:\\KURSHITHA\\Notepad\\student.det","r")

        s=" "

        print("Roll  Name      Gender STD Section:")

        while s:

            s=f.readline()

            s=s.rstrip("\n")

            a=s.split(" ")

            for i in a:

                print(i,end='\t')

            print()

        f.close()

     

    def SEARCH():

        f=open("D:\\KURSHITHA\\Notepad\\student.det","r")

        s=" "

        flag=0

        sroll=input("Enter the searching roll number:")

        while s:

            s=f.readline()

            if sroll in s:

                print("Searching record details:",s)

                flag+=1

        if flag==0:

            print("searching record not found")

        f.close()

     

    CREATE()#invoke create function

    while True:

        print("A. Append\nS. Search\nD. Display\nE. Exit")

        ch=input("Enter Your choice:")

        if ch in 'Aa':

            APPEND()

        elif ch in 'sS':

            SEARCH()

        elif ch in 'dD':

            PRINT()

        elif ch in 'eE':

            break

        else:

            print("Invalid choice")

Output:

Before running the program:

After running the program:




Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me