-
-
Save alimogh/215c6481f999fbc299e608e572e2857f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import close | |
import sys | |
import getopt | |
def add(name, quantity, price): | |
with open('data', 'a') as f: | |
f.write(f"{name}-{quantity}-{price}\n") | |
def delete(name): | |
content = open('data', 'r') | |
items = content.readlines() | |
content.close() | |
f = open('data', 'w') | |
for item in items: | |
if not(name in item.split('-')[0]): | |
f.write(item) | |
f.close() | |
def print_list(): | |
with open('data', 'r') as f: | |
print("Name", "Quantity", "Price") | |
print(f.read().replace('-', '\t')) | |
def main(argv): | |
#try to parse input argument | |
try: | |
opts, args = getopt.getopt(argv, "ga:d:", ["add=", "del="]) | |
except getopt.GetoptError: | |
print("[-a][--a] to enter new value and [-g] to print list") | |
for opt ,arg in opts: | |
if opt in ["-a", "--add"]: | |
input_data = (arg.split('-')) | |
add(input_data[0], input_data[1], input_data[2]) | |
elif opt in ["-d", "--del"]: | |
delete(arg) | |
elif opt == "-g": | |
print_list() | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment