-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.py
81 lines (62 loc) · 2.19 KB
/
Tools.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import os
def readthedict(dictname,mypath='none'):
"""Read the json file specified in mydict
mydict = 'myfile.json' """
if mypath == 'none':
path = os.path.dirname(__file__)
else:
path = mypath
pathandname = os.path.join(path, dictname)
try:
with open(pathandname, "r") as source:
readeddict = json.load(source)
except Exception as mes:
print('Oops! something went wrong when reading json : ' + str(mes))
readeddict = {}
return readeddict
def readthestyle(stylename):
"""Read the css file specified in mydict
mydict = 'mystyle.css' """
path = os.path.dirname(__file__)
pathandname = os.path.join(path, stylename)
try:
with open(pathandname, "r") as source:
readedstyle = source.read()
except Exception as mes:
print('Oops! something went wrong when reading style : ' + str(mes))
readedstyle = ''
return readedstyle
def writethedict(mydict, dictname='poub.json', mypath='none'):
"""Write the dict in the json file in dictname = 'myfile.json'"""
if mypath == 'none':
path = os.path.dirname(__file__)
else:
if not os.path.exists(mypath):
os.makedirs(mypath)
path = mypath
pathandname = os.path.join(path, dictname)
try:
with open(pathandname, "w") as source:
json.dump(mydict, source, indent=4)
answer = True
except Exception as mes:
print('Oops! something went wrong when writing json : ' + str(mes))
answer = False
return answer
def writedictinTxt(mystring, filename='poub.txt', mypath='none'):
if mypath == 'none':
path = os.path.dirname(__file__)
else:
if not os.path.exists(mypath):
os.makedirs(mypath)
path = mypath
pathandname = os.path.join(path, filename)
fichier = open(pathandname, "w")
fichier.write(mystring)
fichier.close()
if __name__ == '__main__':
duck = {'duck': 'walk like a duck'}
if writethedict(duck, 'duck.json'):
isaduck = readthedict('duck.json')
print(isaduck)