Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. # import os module to use remove and rename functions needed in modify_records() function
  2. import os
  3.  
  4.  
  5. def main():
  6.  
  7. # call the append function append_records only if want to add some files to it
  8. # create a False bool variable
  9. append_mode = False
  10. # create the trigger variable which enables the append mode to be ON
  11. append_trigger = input("Do you want to append some record: ")
  12.  
  13. if append_trigger in ['y', "Y", 'yes', 'Yes']:
  14. # activate the append mode it the trigger is on
  15. append_mode = True
  16. # append data if the append mode is activated
  17. if append_mode:
  18. append_records()
  19.  
  20. # call the read function show_records if wanted to display the data on the screem
  21.  
  22. display_mode = False
  23. display_trigger = input("Do you want to display the data: ")
  24. if display_trigger in ['y', 'Y', 'Yes', 'yes']:
  25. display_mode = True
  26.  
  27. if display_mode:
  28. show_records()
  29.  
  30. # call the search function search_record only if an search activator is triggered
  31.  
  32. search_mode = False
  33. search_trigger = input("Do you want to search something: ")
  34. if search_trigger in ['y', 'Y', "Yes", 'yes']:
  35. search_mode = True
  36. if search_mode:
  37. search_records()
  38.  
  39. # call the modify_records function if want to modify a value:
  40.  
  41. modify_mode = False
  42. modify_trigger = input("Do you want to modify something: ")
  43. if modify_trigger in ['y', 'Y', 'Yes', 'yes']:
  44. modify_mode = True
  45. if modify_mode:
  46. modify_records()
  47.  
  48. # call delete_records function if you want to delete an entry:
  49. delete_mode = False
  50. delete_trigger = input("Do you want to detele a record: ")
  51. if delete_trigger in ['y', 'Y', 'Yes', 'yes']:
  52. delete_mode = True
  53. if delete_mode:
  54. delete_records()
  55.  
  56. print("\nAll operations finished!")
  57. r_path = "Starting out with Python - EXERCITII/Cap 7/Coffee Records.txt"
  58. print("You can find your file at the", r_path, 'path')
  59.  
  60.  
  61. def append_records():
  62. """
  63. Description—a string containing the name of the coffee
  64. Quantity in inventory—the number of pounds in inventory, as a floating-point number
  65. Your first job is to write a program that can be used to add records to the file
  66. :return: Nothing
  67. """
  68.  
  69. with open("Coffee Records.txt", mode='a') as coffee_records:
  70. # create an infinite while loop which allows to write/append at that file
  71. another_record = input('Record to append?\n["Y"/"N"]: ')
  72. if another_record in ['y', 'Y']:
  73. print("Enter the following data: ")
  74. while another_record == 'y' or another_record == 'Y':
  75. coffee_description = input("Type: ")
  76. coffee_quantity = int(input("Quantity [kilos]: "))
  77. coffee_records.write(coffee_description + "\n")
  78. coffee_records.write(str(coffee_quantity) + "\n")
  79.  
  80. another_record = input('Append a record?\n["Y"/"N"] ')
  81.  
  82. print("Data appended to 'Coffee Records.txt'\n")
  83.  
  84.  
  85. def show_records():
  86. """
  87. write a program that displays all of the records in the inventory file.
  88. :return: two lists containing the information from the file
  89. """
  90.  
  91. # open the file with the 'open' statement in read mode
  92. coffee_records = open("Coffee Records.txt", mode='r')
  93.  
  94. # crate 2 empty lists to hold the descriptions and quantity files
  95. description_list = []
  96. quantity_list = []
  97.  
  98. # read the first line of the Coffee Records file
  99. description_field = coffee_records.readline()
  100.  
  101. # loop through the file and display the results
  102. while description_field != "":
  103. quantity_field = float(coffee_records.readline())
  104. description_field = description_field.strip()
  105.  
  106. # print the data to the screen
  107. print("DESCRIPTION: ", description_field)
  108. print("QUANTITY:", quantity_field)
  109.  
  110. # append the info to the lists
  111. description_list.append(description_field)
  112. quantity_list.append(quantity_field)
  113.  
  114. # read another description to end the loop if there is none
  115. description_field = coffee_records.readline()
  116.  
  117. coffee_records.close()
  118.  
  119. return [description_list, quantity_list]
  120.  
  121.  
  122. def search_records():
  123. """
  124. enter a description and see a list of all the records matching that description
  125. :return: Nothing
  126. """
  127.  
  128. # create a flag - a bool variable (switch it to True if an item was found):
  129. found = False
  130. search_word = input("Enter a description to search for: ")
  131.  
  132. # open the file in read mode
  133. coffee_records = open("Coffee Records.txt", 'r')
  134. description_field = coffee_records.readline()
  135.  
  136. while description_field != "":
  137. # read the quantity field
  138. quantity_filed = float(coffee_records.readline())
  139.  
  140. # strip the new line from the description field
  141. description_field = description_field.rstrip("\n")
  142.  
  143. # see if the description record matches the search word
  144. if description_field == search_word:
  145. # Display the record:
  146. print("DESCRIPTION:", description_field)
  147. print("QUANTITY:", quantity_filed)
  148. print()
  149. # set the flag to TRUE:
  150. found = True
  151.  
  152. # read the next description / to finish the loop
  153. description_field = coffee_records.readline()
  154.  
  155. # close the file
  156. coffee_records.close()
  157.  
  158. # display something if the search word is not in the file
  159. if not found:
  160. print(f"{search_word} is not in this record!")
  161.  
  162.  
  163. def modify_records():
  164. """
  165. write a program that she can use to modify the quantity field in an existing record.
  166. - need to import os
  167. :return: Nothing
  168. """
  169.  
  170. # create a bool variable to use as a flag - when found the searched value
  171. found = False
  172.  
  173. search = input("Enter a description to search for: ")
  174. new_quantity = float(input("Enter the new quantity: "))
  175.  
  176. # open the original Coffee Records.txt file in read mode
  177. coffee_records = open("Coffee Records.txt", 'r')
  178. # create a temporary file in write mode
  179. temp_file = open("temp.txt", 'w')
  180.  
  181. # read the first description in the original file and compare it with an empty string
  182. description_filed = coffee_records.readline()
  183.  
  184. while description_filed != "":
  185. # read the rest of the file and strip the newline form the description filed
  186. quantity_filed = float(coffee_records.readline())
  187. description_filed = description_filed.rstrip("\n")
  188.  
  189. # modify the quantity and write it to the temp file if search is found
  190. if description_filed == search:
  191. temp_file.write(description_filed + "\n")
  192. temp_file.write(str(new_quantity) + '\n')
  193.  
  194. # set the flag to True
  195. found = True
  196.  
  197. else:
  198. # write the original data to the temp file if not math found
  199. temp_file.write(description_filed + "\n")
  200. temp_file.write(str(quantity_filed) + "\n")
  201.  
  202. # read the new description line
  203. description_filed = coffee_records.readline()
  204.  
  205. # close both files
  206. coffee_records.close()
  207. temp_file.close()
  208.  
  209. # delete the old file - Coffee Records.txt
  210. os.remove("Coffee Records.txt")
  211. # rename the temp file in Coffee Records.txt
  212. os.rename("temp.txt", 'Coffee Records.txt')
  213.  
  214. # display a message if updated or if not found!
  215. if found:
  216. print("The file was updated!")
  217. else:
  218. print(f"{search} was not found in the file!")
  219.  
  220.  
  221. def delete_records():
  222. """
  223. write a program that you can use to delete records from the initial file
  224. :return:Nothing
  225. """
  226.  
  227. # open the original file in read mode to and a temporary file in write mode
  228. coffee_records = open("Coffee Records.txt", 'r')
  229. temp_file = open("Temporary File.txt", 'w')
  230.  
  231. # crate a bool flag to indicate if what I searched is in the file or not (initially set to False)
  232. found = False
  233.  
  234. # define the search item you want to delete
  235. search_item = input("What record do you want to delete: ")
  236.  
  237. # read the first entry from the original file and compare it with an empty string:
  238. description_filed = coffee_records.readline()
  239. while description_filed != "":
  240. # read quantity - second line of the initial file and delete the empty space
  241. quantity_field = float(coffee_records.readline())
  242. description_filed = description_filed.rstrip("\n")
  243.  
  244. if description_filed != search_item:
  245. temp_file.write(description_filed + "\n")
  246. temp_file.write(str(quantity_field) + "\n")
  247. else:
  248. found = True
  249.  
  250. description_filed = coffee_records.readline()
  251.  
  252. # close the files
  253. coffee_records.close()
  254. temp_file.close()
  255.  
  256. os.remove("Coffee Records.txt")
  257. os.rename("Temporary File.txt", "Coffee Records.txt")
  258.  
  259. if found:
  260. print(f"{search_item} was deleted from your file!")
  261. else:
  262. print(f"{search_item} not in your file!")
  263.  
  264.  
  265. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement