Advertisement
ALENTL

main.py

Jul 26th, 2022
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. import pickle as p, os
  2.  
  3. def create():
  4.     f = open('mobile.dat', 'wb')
  5.     n = int(input("Enter no of records: "))
  6.     print("\n")
  7.  
  8.     for k in range(n):
  9.         mod = input("Enter model: ")
  10.         comp = input("Enter company: ")
  11.         price = int(input("Enter price: "))
  12.         print("\n")
  13.  
  14.         D = {"Model": mod, "Company": comp, "Price": price}
  15.  
  16.         p.dump(D, f)
  17.  
  18.     f.close()
  19.     print("File Created")
  20.  
  21. def tabular():
  22.     f = open('mobile.dat', 'rb')
  23.     print("{:<20}".format("Model"), "{:<20}".format("Company"), "{:<20}".format("Price"))
  24.  
  25.     while True:
  26.         try:
  27.             A = p.load(f)
  28.  
  29.             for k in A:
  30.                 print("{:<20}".format(A[k]), end=" ")
  31.             print()
  32.  
  33.         except EOFError:
  34.             break
  35.  
  36.     f.close()
  37.  
  38. def append():
  39.     f = open('mobile.dat', 'ab')
  40.  
  41.     mod = input("Enter model: ")
  42.     comp = input("Enter company: ")
  43.     price = input("Enter price: ")
  44.     print("\n")
  45.  
  46.     D = {"Model": mod, "Company": comp, "Price": price}
  47.  
  48.     p.dump(D, f)
  49.  
  50.     f.close()
  51.     print("File Appended")
  52.  
  53.     tabular()
  54.  
  55. def delete():
  56.     f = open('mobile.dat', 'rb')
  57.     f1 = open("mobile1.dat", 'wb')
  58.  
  59.     comp = input("Enter company to check: ")
  60.  
  61.     while True:
  62.         try:
  63.             A = p.load(f)
  64.  
  65.             if A["Company"] != comp:
  66.                 p.dump(A, f1)
  67.         except EOFError:
  68.             break
  69.  
  70.     f.close()
  71.     f1.close()
  72.  
  73.     os.remove('mobile.dat')
  74.     os.rename('mobile1.dat', 'mobile.dat')
  75.  
  76.     tabular()
  77.  
  78. while True:
  79.     print("\t\t\t1. Create a binary file")
  80.     print("\t\t\t2. Display the content of the file in tabular form")
  81.     print("\t\t\t3. Input data and append it into the binary file")
  82.     print("\t\t\t4. Delete all details of the mobile for a given company")
  83.     print("\t\t\t5. Kill the Program\n")
  84.  
  85.     ch = int(input("\t\t\tEnter your selection: "))
  86.     print("\n")
  87.  
  88.     if ch == 1:
  89.         create()
  90.  
  91.     elif ch == 2:
  92.         tabular()
  93.  
  94.     elif ch == 3:
  95.         append()
  96.  
  97.     elif ch == 4:
  98.         delete()
  99.  
  100.     elif ch == 5:
  101.         print("Thank you for using me")
  102.         break
  103.  
  104.     else:
  105.         print("Invalid option, Please try again")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement