-
Notifications
You must be signed in to change notification settings - Fork 0
/
include.py
37 lines (36 loc) · 1.32 KB
/
include.py
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
27
28
29
30
31
32
33
34
35
36
37
import os
import json
from sys import argv
from sys import stderr
def read():
try:
os.mkdir(os.path.expanduser("~/.vocab"))
except FileExistsError:
pass
try:
with open(os.path.expanduser("~/.vocab/data.json"),"r") as f:
try:
words=json.load(f)
except json.decoder.JSONDecodeError:
print(os.path.expanduser("~/.vocab/data.json"+" can't be decoded."),file=stderr)
print("See if it's set to a wrong authority",file=stderr)
print("or the file is broken",file=stderr)
print("Type Y to clear the file.", file=stderr)
if input() == 'Y':
write([])
words=[]
else:
exit()
return words
except FileNotFoundError:
print(os.path.expanduser("~/.vocab/data.json"), "is not found.", file=stderr)
print("Initialize it with an empty list", file=stderr)
write([])
return []
def write(words):
try:
with open(os.path.expanduser("~/.vocab/data.json"),"w") as f:
json.dump((words),f)
except FileNotFoundError:
print(os.path.expanduser("~/.vocab/data.json"+" is not found."),file=stderr)
print("call "+argv[0]+" --help for more information",file=stderr)