Advertisement
Zeinab_Hamdy

Untitled

Apr 15th, 2023
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. def writeStudent():
  2.     with open("Student.txt" ,'a') as file :
  3.         ch ='Y'
  4.         while ch =='Y':
  5.             Id=input('Enter the id : ')
  6.             Name=input('Enter the name : ')
  7.             Age=input('Enter the age : ')
  8.             file.write(Id + '\t' + Name + '\t' + Age +'\n')
  9.             ch = input('if u want to continue [ Y/N ] : ')
  10.  
  11. def readStudent() :
  12.     with open("Student.txt" ,'r' ) as file :
  13.         print("Id\tName\tAge\n")
  14.         for line in file :
  15.             print(line , end='')
  16.  
  17.  
  18. def updateStudent() :
  19.     import os
  20.     ID=input('Enter the id for update its age :')
  21.     with open("Student.txt" ,'r' ) as file :
  22.         with open("StudentTemp.txt" , 'a') as temp :
  23.          for line in file :
  24.              L = line.split('\t')
  25.              if ID==L[0] : ## this record
  26.                  Age= input('Enter the new age : ')
  27.                  L[2]= Age
  28.                 # line = L[0] + '\t' + L[1] + '\t' + L[2] + '\n'
  29.              temp.write(L[0] + '\t' + L[1] + '\t' + L[2] + '\n' )
  30.                    
  31.     os.remove("Student.txt")
  32.     os.rename("StudentTemp.txt" ,"Student.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement