Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("**Welcome**")
- t="y"
- while t=='y':
- print("Enter the number of opertion you want")
- print("1-Write on the file")
- print("2-Read from the file")
- print("3-search on the file")
- print("4-update on the file")
- print("5-Delete from the file")
- s = int(input("Enter the number of operation you want to do "))
- match s:
- case 1:
- c="y"
- with open('carpy.txt', 'w') as file:
- while c == 'y':
- id = input('Enter ID: ')
- name = input('Enter Name: ')
- model = input('Enter Model: ')
- ownerofcar = input('Enter the owner of car: ')
- file.write(id + '\t' + name + '\t' + model + '\t' + ownerofcar + '\n')
- print('File saved successfully')
- c = input("Enter another record? (y/n)")
- t = input("Do you want to do anothe opertion (y/n):")
- case 2:
- with open('carpy.txt', 'r') as file:
- print('ID\tName\tModel\tOwnerofcar')
- print('----------------------------------------')
- for line in file:
- print(line, end='\n')
- t = input("Do you want to do anothe opertion (y/n):")
- case 3:
- id = int(input('Enter the id to search for: '))
- with open('carpy.txt', 'r') as file:
- found = False
- for line in file:
- fields = line.split('\t')
- if fields[0] == id:
- found = True
- print('ID\tName\tModel\tOwnerofcar')
- print('----------------------------------------')
- print(line)
- if not found:
- print('car not found')
- t = input("Do you want to do anothe opertion (y/n):")
- case 5:
- import os
- id = input('Enter the id of car you want to delete: ')
- file = open('carpy.txt','r')
- tempfile = open('tempcarpy.txt', 'w')
- flag = False
- for line in file:
- fields = line.split("\t")
- if fields[0] == id:
- flag = True
- else:
- tempfile.write(line)
- file.close()
- tempfile.close()
- os.remove('carpy.txt')
- os.rename('tempcarpy.txt', 'carpy.txt')
- if flag:
- print("record successfuly deleted")
- else:
- print("No student are matched ")
- t = input("Do you want to do anothe opertion (y/n):")
- case 4:
- import os
- id = input('Enter the id of car you want to delete: ')
- file = open('carpy.txt', 'r')
- tempfile = open('tempcarpy.txt', 'w')
- flag = False
- for line in file:
- fields = line.split("\t")
- if fields[0] == id:
- flag = True
- name = input("Enter The new car name ")
- model =input("Enter The new car model ")
- ownerofcar = input("Enter The new owner name ")
- line = fields[0] + '\t' + name + '\t' + model + '\t' + ownerofcar +'\n'
- tempfile.write(line)
- file.close()
- tempfile.close()
- os.remove('carpy.txt')
- os.rename('tempcarpy.txt', 'carpy.txt')
- if flag:
- print("record successfuly updated")
- else:
- print("No student are matched ")
- t = input("Do you want to do anothe opertion (y/n):")
- print("**THE END**")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement