90.9k views
0 votes
5. Modify the file by allowing the owner to remove data from the file: a. Ask the owner to enter a description to remove b. If the description exists, remove the coffee name and the quantity c. If the description is not found, display the message: That item was not found in the file. 6. Modify the file by allowing the owner to delete data from the file: a. Ask the owner to enter a description to delete b. If the description exists, delete the coffee name and the quantity c. Replace the name and quantity of the coffee removed in step b by asking the user to enter a new coffee name and quantity d. If the description i

2 Answers

2 votes

Answer:

See explaination

Step-by-step explanation:

#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()

User Pavan Dittakavi
by
5.2k points
5 votes

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()

User Matt Lohkamp
by
4.4k points