/
Util.py
67 lines (60 loc) · 2 KB
/
Util.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
import os
import sys
import json
def AssertDir(directory):
if not os.path.isdir(directory):
outString = "**ERROR** Directory " + directory + " does not exist."
sys.exit(outString)
def SaveJson(directory, filename, source):
fullPath = directory + '/' + filename
if os.path.isfile(fullPath):
outString = " *Error* File attempting to save: " + fullPath + " already exists."
print(outString)
else:
outString = "Writing file: " + fullPath
print(outString)
with open(fullPath, 'w') as outfile:
json.dump(source, outfile)
def LoadJson(directory, filename, quitOnFail):
AssertDir(directory)
fullPath = directory + '/' + filename
if not os.path.isfile(fullPath):
outString = " *Error* File attempting to load: " + fullPath + " does not exist."
print(outString)
if quitOnFail:
sys.exit()
else:
outString = "Loading file: " + fullPath
print(outString)
with open(fullPath, 'r') as file:
return json.loads(file.read())
def MakeDir(directory):
if os.path.isdir(directory):
outString = "Directory " + directory + " already exists."
else:
os.mkdir(directory)
outString = "Directory " + directory + " created."
print(outString)
def AppendDateAndTime(timestamp, string):
return timestamp.strftime("%Y-%m-%d-%H-%M-%S_") + string
def SaveTextFile(directory, filename, source):
fullPath = directory + '/' + filename
if os.path.isfile(fullPath):
outString = " *Error* File attempting to save: " + fullPath + " already exists."
print(outString)
else:
outString = "Writing file: " + fullPath
print(outString)
with open(fullPath, 'w', newline='') as dataOut:
dataOut.write(source)
def LoadTextFile(directory, filename):
fullPath = directory + '/' + filename
if not os.path.isfile(fullPath):
outString = " *Error* File attempting to load: " + fullPath + " does not exist."
print(outString)
return ''
else:
outString = "Loading file: " + fullPath
print(outString)
with open(fullPath, 'r') as file:
return file.read()