-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathUtils.py
More file actions
66 lines (51 loc) · 1.68 KB
/
Utils.py
File metadata and controls
66 lines (51 loc) · 1.68 KB
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
# coding:utf-8
import os
import sys
try:
import configparser
except Exception as e:
print("module is't installed,please used 'pip install configparser' commmond to install it")
# 返回当前脚本的全路径,末尾不带\
def getthispath():
path = sys.path[0]
# 判断为脚本文件还是py2exe编译后的文件,如果是脚本文件,则返回的是脚本的目录,如果是py2exe编译后的文件,则返回的是编译后的文件路径
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.split(path)[0]
def get_file_ext(path):
print(os.path.splitext(path))
# return os.path.splitext(path)[1]
# 获取路径的父目录,末尾不带\
def getparent(filepath):
if not filepath:
return None
lsPath = os.path.split(filepath)
# print(lsPath)
# print("lsPath[1] = %s" %lsPath[1])
if lsPath[1]:
return lsPath[0]
lsPath = os.path.split(lsPath[0])
return lsPath[0]
# 创建多级目录,比如c:\\test1\\test2,如果test1 test2都不存在,都将被创建
def create_dirs(to_create_path):
path_create = to_create_path
if os.sep == '\\':
path_create = path_create.replace('/', os.sep)
dirs = path_create.split(os.sep)
path = ''
for dir in dirs:
dir += os.sep
path = os.path.join(path, dir)
if not os.path.exists(path):
os.mkdir(path, 0o777)
if not os.path.exists(to_create_path):
return False
return True
def delete_file(to_del_file):
if os.path.exists(to_del_file):
os.remove(to_del_file)
if not os.path.exists(to_del_file):
return True
else:
return False