-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
29 lines (22 loc) · 785 Bytes
/
storage.py
File metadata and controls
29 lines (22 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import argparse
import os
import tempfile
import json
storage_path = os.path.join(tempfile.gettempdir(), 'storage.data')
parser = argparse.ArgumentParser()
parser.add_argument("--key", help="key of elements")
parser.add_argument("--value", help="value of elements")
args = parser.parse_args()
data = []
if os.path.exists(storage_path) and os.stat(storage_path).st_size != 0:
with open(storage_path, 'r') as f:
data = json.loads(f.read())
if args.key and args.value:
data.append({args.key: args.value})
with open(storage_path, 'w') as f:
f.write(json.dumps(data))
elif args.key:
result_list = [item[args.key] for item in data if args.key in item]
print(*result_list, sep=', ')
else:
print('The program is called with invalid parameters.')