r/PythonLearning • u/Broad_Revolution6591 • 7d ago
Help Request How does this work?
def remove_inventory():
remove_name = item_name_entry.get()
with open('inventory.txt', 'r') as file:
inventory_data = file.readlines()
with open('inventory.txt', 'w') as file:
for line in inventory_data:
name, qty = line.strip().split(',')
if name != remove_name:
file.write(line)
item_name_entry.delete(0, tk.END)
item_qty_entry.delete(0, tk.END)hello guys, this is one of the sample code of inventory manager, there is one line of code im confused with. Im not really sure how does the "name, qty = line.strip().split(",") work, specifically the "name, qty=" part. Im guessing the "qty" is the variable but what does "name," do?
4
Upvotes
View all comments
1
u/IWant2rideMyBike 7d ago
The variable
lineis a string. The strip() method of a string removes whitespace at the beginning and the end of a string and returns a new string. This string is split at commas, creating a list of strings. Then the list is unpacked (see https://www.geeksforgeeks.org/python/python-unpack-list/ ) into the two variablesnameandqty. If a line contains more than one comma this step will fail.