Advertisement
sherry_ahmos

Untitled

May 18th, 2022
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. print("**Welcome**")
  2. t="y"
  3. while t=='y':
  4.     print("Enter the number of opertion you want")
  5.     print("1-Write on the file")
  6.     print("2-Read from the file")
  7.     print("3-search on the file")
  8.     print("4-update on the file")
  9.     print("5-Delete from the file")
  10.     s = int(input("Enter the number of operation you want to do "))
  11.     match s:
  12.         case 1:
  13.             c="y"
  14.             with open('carpy.txt', 'w') as file:
  15.                 while c == 'y':
  16.                     id = input('Enter ID: ')
  17.                     name = input('Enter Name: ')
  18.                     model = input('Enter Model: ')
  19.                     ownerofcar = input('Enter the owner of car: ')
  20.                     file.write(id + '\t' + name + '\t' + model + '\t' + ownerofcar + '\n')
  21.                     print('File saved successfully')
  22.                     c = input("Enter another record? (y/n)")
  23.             t = input("Do you want to do anothe opertion (y/n):")
  24.         case 2:
  25.             with open('carpy.txt', 'r') as file:
  26.                 print('ID\tName\tModel\tOwnerofcar')
  27.                 print('----------------------------------------')
  28.                 for line in file:
  29.                     print(line, end='\n')
  30.             t = input("Do you want to do anothe opertion (y/n):")
  31.         case 3:
  32.             id = int(input('Enter the id to search for: '))
  33.             with open('carpy.txt', 'r') as file:
  34.                 found = False
  35.                 for line in file:
  36.                     fields = line.split('\t')
  37.                     if fields[0] == id:
  38.                         found = True
  39.                         print('ID\tName\tModel\tOwnerofcar')
  40.                         print('----------------------------------------')
  41.                         print(line)
  42.                 if not found:
  43.                     print('car not found')
  44.             t = input("Do you want to do anothe opertion (y/n):")
  45.         case 5:
  46.             import os
  47.             id = input('Enter the id of car you want to delete: ')
  48.             file = open('carpy.txt','r')
  49.             tempfile = open('tempcarpy.txt', 'w')
  50.             flag = False
  51.             for line in file:
  52.                 fields = line.split("\t")
  53.                 if fields[0] == id:
  54.                     flag = True
  55.                 else:
  56.                     tempfile.write(line)
  57.             file.close()
  58.             tempfile.close()
  59.             os.remove('carpy.txt')
  60.             os.rename('tempcarpy.txt', 'carpy.txt')
  61.             if flag:
  62.                 print("record successfuly deleted")
  63.             else:
  64.                 print("No student are matched ")
  65.             t = input("Do you want to do anothe opertion (y/n):")
  66.         case 4:
  67.             import os
  68.             id = input('Enter the id of car you want to delete: ')
  69.             file = open('carpy.txt', 'r')
  70.             tempfile = open('tempcarpy.txt', 'w')
  71.             flag = False
  72.             for line in file:
  73.                 fields = line.split("\t")
  74.                 if fields[0] == id:
  75.                     flag = True
  76.                     name = input("Enter The new car name ")
  77.                     model =input("Enter The new car model ")
  78.                     ownerofcar = input("Enter The new owner name ")
  79.                     line = fields[0] + '\t' + name + '\t' + model + '\t' + ownerofcar +'\n'
  80.                 tempfile.write(line)
  81.             file.close()
  82.             tempfile.close()
  83.             os.remove('carpy.txt')
  84.             os.rename('tempcarpy.txt', 'carpy.txt')
  85.             if flag:
  86.                 print("record successfuly updated")
  87.             else:
  88.                 print("No student are matched ")
  89.             t = input("Do you want to do anothe opertion (y/n):")
  90. print("**THE END**")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement