Answer:
Check the explanation
Step-by-step explanation:
Code:
# 3------reading Coffe shop Inventory file
f = open('coffeeInventory.txt','r+')#opening file in reading mode
f1 = f.readlines()
numPounds = 0
for i in range(len(f1)):
print(f1[i].split('\\')[0])
if i!=0:
numPounds+=(int((f1[i].split('\\')[0]).split(';')[1]))
#Total pounds of coffee
print("Total pounds of Coffee:",numPounds)
f.close()#closing file
#4---------
f = open('coffeeInventory.txt','a+')#opening file in appending mode
#appending records
f.write('\\Guatemala Antigua;22')
f.write('\\House Blend;25')
f.write('\\Decaf House Blend;16')
f.close()#closing file
#5--------
#removing the record as per owner's description
Description = input("enter Description to remove:")
f = open('coffeeInventory.txt','r')#opening files in reading mode
f1 = f.readlines()
found = -1
for i in range(len(f1)):
line = f1[i].split('\\')[0]
des = line.split(';')[0]
if des==Description:
found = i
#storing the previous data
f2= open('coffeeInventory_pre.txt','w+')
f2.writelines(f1)
f2.close()
#removing the data if found
if found==-1:
print("The item was not found in the file")
else:
f1.remove(f1[found])
data_list = f1
f.close()
#modifying file after removing
modify_file = open('coffeeInventory.txt','w+')
for x in data_list:
modify_file.write(x)
modify_file.close()
f.close()
#6--------------
#deleting the record as per owner's description
Description = input("enter Description to delete:")
f = open('coffeeInventory.txt','r')#opening files in reading mode
f1 = f.readlines()
found = -1
for i in range(len(f1)):
line = f1[i].split('\\')[0]
des = line.split(';')[0]
if des==Description:
found = i
if i==-1:
print("The item was not found in the file")
else:
f1.remove(f1[found])
data_list = f1
f.close()
# deleting in the pre_Coffee_inventory file
#finding record
f2 = open('coffeeInventory_pre.txt','r')
f3 =f2.readlines()
found = -1
for i in range(len(f3)):
line = f3[i].split('\\')[0]
des = line.split(';')[0]
if des==Description:
found = i
if i==-1:
print("The item was not found in the file")
else:
f3.remove(f3[found])#deleting record
data_list1 = f3
f2.close()
#modifying pre file
f2 = open('coffeeInventory_pre.txt','w+')
f2.writelines(data_list1)
f2.close()
#opening file in writing mode
modify_file = open('coffeeInventory.txt','w+')
modify_file.writelines(data_list)
modify_file.close()