Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle as p, os
- def create():
- f = open('mobile.dat', 'wb')
- n = int(input("Enter no of records: "))
- print("\n")
- for k in range(n):
- mod = input("Enter model: ")
- comp = input("Enter company: ")
- price = int(input("Enter price: "))
- print("\n")
- D = {"Model": mod, "Company": comp, "Price": price}
- p.dump(D, f)
- f.close()
- print("File Created")
- def tabular():
- f = open('mobile.dat', 'rb')
- print("{:<20}".format("Model"), "{:<20}".format("Company"), "{:<20}".format("Price"))
- while True:
- try:
- A = p.load(f)
- for k in A:
- print("{:<20}".format(A[k]), end=" ")
- print()
- except EOFError:
- break
- f.close()
- def append():
- f = open('mobile.dat', 'ab')
- mod = input("Enter model: ")
- comp = input("Enter company: ")
- price = input("Enter price: ")
- print("\n")
- D = {"Model": mod, "Company": comp, "Price": price}
- p.dump(D, f)
- f.close()
- print("File Appended")
- tabular()
- def delete():
- f = open('mobile.dat', 'rb')
- f1 = open("mobile1.dat", 'wb')
- comp = input("Enter company to check: ")
- while True:
- try:
- A = p.load(f)
- if A["Company"] != comp:
- p.dump(A, f1)
- except EOFError:
- break
- f.close()
- f1.close()
- os.remove('mobile.dat')
- os.rename('mobile1.dat', 'mobile.dat')
- tabular()
- while True:
- print("\t\t\t1. Create a binary file")
- print("\t\t\t2. Display the content of the file in tabular form")
- print("\t\t\t3. Input data and append it into the binary file")
- print("\t\t\t4. Delete all details of the mobile for a given company")
- print("\t\t\t5. Kill the Program\n")
- ch = int(input("\t\t\tEnter your selection: "))
- print("\n")
- if ch == 1:
- create()
- elif ch == 2:
- tabular()
- elif ch == 3:
- append()
- elif ch == 4:
- delete()
- elif ch == 5:
- print("Thank you for using me")
- break
- else:
- print("Invalid option, Please try again")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement