Только недавно начал учить python. Задача состоит в том, чтобы создать key-value хранилище. При запуске только с ключом требуется вернуть его значение из файла, если на вход подается ключ и значение то добавить в файл. Ошибка при попытке запустить с ключом и значением. Подскажите в чем проблема?
import sys
import argparse
import os
import tempfile
import json
def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--key')
parser.add_argument('--value')
return parser
parser = create_parser()
namespace = parser.parse_args()
str = None
if namespace.value is None:
storage_filename = os.path.join(tempfile.gettempdir(), 'storage.data')
if os.path.exists('storage.data'):
with open(storage_filename, 'r') as f:
json_string = f.readlines()
parsed_str = json.loads(json_string)
bool = False
for arg in parsed_str[namespace.key]:
str = ", " + arg if bool is True else arg
bool = True
print(str)
else:
print("File not found")
else:
storage_filename = os.path.join(tempfile.gettempdir(), 'storage.data')
if os.path.exists('storage.date'):
with open(storage_filename, 'a') as f:
f.write(dict({str(namespace.key): str(namespace.value)}))
else:
with open(storage_filename, 'w') as f:
f.write(dict({str(namespace.key): str(namespace.value)}))
Ошибка:
C:\Users\Aleksey>python "C:\Users\Aleksey\playground\Key-value хранилище.py" --key Astrakhan --value 1
Traceback (most recent call last):
File "C:\Users\Aleksey\playground\Key-value хранилище.py", line 39, in <module>
f.write(dict({str(namespace.key): str(namespace.value)}))
TypeError: 'NoneType' object is not callable