-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.py
More file actions
23 lines (21 loc) · 894 Bytes
/
file.py
File metadata and controls
23 lines (21 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import ErrorHook,json,os
class File():
def __init__(self,fileType:str=['json','plain']):
if not fileType in ['json','plain']:
raise NameError("fileType 值应在 ['json','plain'] 中,找到{}".format(fileType))
self.type=fileType
self.path=os.path.dirname(os.path.abspath(__file__))
def read(self,path,absPath=False) -> dict:
with open(("" if absPath else self.path)+path,'r',encoding='utf-8') as f:
if self.type=='json':
return json.load(f)
else:
return f.read()
def save(self,path,file,absPath=False,fileType='w'):
with open(("" if absPath else self.path)+path,fileType,encoding='utf-8') as f:
if self.type=='json':
json.dump(file,f)
else:
f.write(str(file))
if __name__=='__main__':
File('json')