-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
51 lines (38 loc) · 925 Bytes
/
utils.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
定义几个函数 写文件 通知 返回键 创建目录 创建文件
"""
import os
import platform
import subprocess
from collections import OrderedDict
__all__ = [
"utf8_data_to_file",
"notify",
"uniq",
"create_file",
]
def utf8_data_to_file(f, data):
if hasattr(data, "decode"):
f.write(data.decode("utf-8"))
else:
f.write(data)
def mkdir(path):
try:
os.mkdir(path)
return True
except OSError:
return False
def create_dir(path):
if not os.path.exists(path):
return mkdir(path)
elif os.path.isdir(path):
return True
else:
os.remove(path)
return mkdir(path)
def create_file(path, default="\n"):
if not os.path.exists(path):
with open(path, "w") as f:
f.write(default)