Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import os module to use remove and rename functions needed in modify_records() function
- import os
- def main():
- # call the append function append_records only if want to add some files to it
- # create a False bool variable
- append_mode = False
- # create the trigger variable which enables the append mode to be ON
- append_trigger = input("Do you want to append some record: ")
- if append_trigger in ['y', "Y", 'yes', 'Yes']:
- # activate the append mode it the trigger is on
- append_mode = True
- # append data if the append mode is activated
- if append_mode:
- append_records()
- # call the read function show_records if wanted to display the data on the screem
- display_mode = False
- display_trigger = input("Do you want to display the data: ")
- if display_trigger in ['y', 'Y', 'Yes', 'yes']:
- display_mode = True
- if display_mode:
- show_records()
- # call the search function search_record only if an search activator is triggered
- search_mode = False
- search_trigger = input("Do you want to search something: ")
- if search_trigger in ['y', 'Y', "Yes", 'yes']:
- search_mode = True
- if search_mode:
- search_records()
- # call the modify_records function if want to modify a value:
- modify_mode = False
- modify_trigger = input("Do you want to modify something: ")
- if modify_trigger in ['y', 'Y', 'Yes', 'yes']:
- modify_mode = True
- if modify_mode:
- modify_records()
- # call delete_records function if you want to delete an entry:
- delete_mode = False
- delete_trigger = input("Do you want to detele a record: ")
- if delete_trigger in ['y', 'Y', 'Yes', 'yes']:
- delete_mode = True
- if delete_mode:
- delete_records()
- print("\nAll operations finished!")
- r_path = "Starting out with Python - EXERCITII/Cap 7/Coffee Records.txt"
- print("You can find your file at the", r_path, 'path')
- def append_records():
- """
- Description—a string containing the name of the coffee
- Quantity in inventory—the number of pounds in inventory, as a floating-point number
- Your first job is to write a program that can be used to add records to the file
- :return: Nothing
- """
- with open("Coffee Records.txt", mode='a') as coffee_records:
- # create an infinite while loop which allows to write/append at that file
- another_record = input('Record to append?\n["Y"/"N"]: ')
- if another_record in ['y', 'Y']:
- print("Enter the following data: ")
- while another_record == 'y' or another_record == 'Y':
- coffee_description = input("Type: ")
- coffee_quantity = int(input("Quantity [kilos]: "))
- coffee_records.write(coffee_description + "\n")
- coffee_records.write(str(coffee_quantity) + "\n")
- another_record = input('Append a record?\n["Y"/"N"] ')
- print("Data appended to 'Coffee Records.txt'\n")
- def show_records():
- """
- write a program that displays all of the records in the inventory file.
- :return: two lists containing the information from the file
- """
- # open the file with the 'open' statement in read mode
- coffee_records = open("Coffee Records.txt", mode='r')
- # crate 2 empty lists to hold the descriptions and quantity files
- description_list = []
- quantity_list = []
- # read the first line of the Coffee Records file
- description_field = coffee_records.readline()
- # loop through the file and display the results
- while description_field != "":
- quantity_field = float(coffee_records.readline())
- description_field = description_field.strip()
- # print the data to the screen
- print("DESCRIPTION: ", description_field)
- print("QUANTITY:", quantity_field)
- # append the info to the lists
- description_list.append(description_field)
- quantity_list.append(quantity_field)
- # read another description to end the loop if there is none
- description_field = coffee_records.readline()
- coffee_records.close()
- return [description_list, quantity_list]
- def search_records():
- """
- enter a description and see a list of all the records matching that description
- :return: Nothing
- """
- # create a flag - a bool variable (switch it to True if an item was found):
- found = False
- search_word = input("Enter a description to search for: ")
- # open the file in read mode
- coffee_records = open("Coffee Records.txt", 'r')
- description_field = coffee_records.readline()
- while description_field != "":
- # read the quantity field
- quantity_filed = float(coffee_records.readline())
- # strip the new line from the description field
- description_field = description_field.rstrip("\n")
- # see if the description record matches the search word
- if description_field == search_word:
- # Display the record:
- print("DESCRIPTION:", description_field)
- print("QUANTITY:", quantity_filed)
- print()
- # set the flag to TRUE:
- found = True
- # read the next description / to finish the loop
- description_field = coffee_records.readline()
- # close the file
- coffee_records.close()
- # display something if the search word is not in the file
- if not found:
- print(f"{search_word} is not in this record!")
- def modify_records():
- """
- write a program that she can use to modify the quantity field in an existing record.
- - need to import os
- :return: Nothing
- """
- # create a bool variable to use as a flag - when found the searched value
- found = False
- search = input("Enter a description to search for: ")
- new_quantity = float(input("Enter the new quantity: "))
- # open the original Coffee Records.txt file in read mode
- coffee_records = open("Coffee Records.txt", 'r')
- # create a temporary file in write mode
- temp_file = open("temp.txt", 'w')
- # read the first description in the original file and compare it with an empty string
- description_filed = coffee_records.readline()
- while description_filed != "":
- # read the rest of the file and strip the newline form the description filed
- quantity_filed = float(coffee_records.readline())
- description_filed = description_filed.rstrip("\n")
- # modify the quantity and write it to the temp file if search is found
- if description_filed == search:
- temp_file.write(description_filed + "\n")
- temp_file.write(str(new_quantity) + '\n')
- # set the flag to True
- found = True
- else:
- # write the original data to the temp file if not math found
- temp_file.write(description_filed + "\n")
- temp_file.write(str(quantity_filed) + "\n")
- # read the new description line
- description_filed = coffee_records.readline()
- # close both files
- coffee_records.close()
- temp_file.close()
- # delete the old file - Coffee Records.txt
- os.remove("Coffee Records.txt")
- # rename the temp file in Coffee Records.txt
- os.rename("temp.txt", 'Coffee Records.txt')
- # display a message if updated or if not found!
- if found:
- print("The file was updated!")
- else:
- print(f"{search} was not found in the file!")
- def delete_records():
- """
- write a program that you can use to delete records from the initial file
- :return:Nothing
- """
- # open the original file in read mode to and a temporary file in write mode
- coffee_records = open("Coffee Records.txt", 'r')
- temp_file = open("Temporary File.txt", 'w')
- # crate a bool flag to indicate if what I searched is in the file or not (initially set to False)
- found = False
- # define the search item you want to delete
- search_item = input("What record do you want to delete: ")
- # read the first entry from the original file and compare it with an empty string:
- description_filed = coffee_records.readline()
- while description_filed != "":
- # read quantity - second line of the initial file and delete the empty space
- quantity_field = float(coffee_records.readline())
- description_filed = description_filed.rstrip("\n")
- if description_filed != search_item:
- temp_file.write(description_filed + "\n")
- temp_file.write(str(quantity_field) + "\n")
- else:
- found = True
- description_filed = coffee_records.readline()
- # close the files
- coffee_records.close()
- temp_file.close()
- os.remove("Coffee Records.txt")
- os.rename("Temporary File.txt", "Coffee Records.txt")
- if found:
- print(f"{search_item} was deleted from your file!")
- else:
- print(f"{search_item} not in your file!")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement