-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileUtil.py
More file actions
78 lines (62 loc) · 1.94 KB
/
FileUtil.py
File metadata and controls
78 lines (62 loc) · 1.94 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
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Author: AsherYang
Email : ouyangfan1991@gmail.com
Date : 2017/12/22
Desc : 文件操作类
http://blog.csdn.net/ziyuzhao123/article/details/8811496
"""
import os
# 从文件路径中提取文件名(包括后缀拓展名)
def getFileName(filePath):
fileName = 'UnKnownFile'
if not filePath:
return fileName
filePath = unicode(filePath)
return os.path.basename(filePath)
# 返回文件拓展名(不包含点".")
def getFileExt(filePath):
fileExt = 'UnknownFileExt'
if not filePath:
return fileExt
filePath = unicode(filePath)
return os.path.splitext(filePath)[1][1:].lower()
# 返回文件名(包括文件目录, 但不包含扩展名)
def getFilePathWithName(filePath):
if not filePath:
return './'
filePath = unicode(filePath)
return os.path.splitext(filePath)[0]
# 返回文件父目录
def getFileDir(filePath):
if not filePath:
return './'
filePath = unicode(filePath)
return os.path.dirname(filePath)
# 获取指定目录及其子目录下, 所有文件
def getAllFiles(dir):
dir = unicode(dir)
fileList = []
for root, dirs, files in os.walk(dir):
for file in files:
fileList.append(os.path.join(root, file))
return fileList
# 获取指定目录及其子目录下,指定文件名的文件
def getAllFilesByExt(dir, fileExt):
fileList = []
dir = unicode(dir)
fileExt = unicode(fileExt)
if not fileExt:
return fileList
for root, dirs, files in os.walk(dir):
for file in files:
if fileExt == getFileExt(file):
fileList.append(os.path.join(root, file))
return fileList
# 当文件目录不存在时,创建一个文件目录(创建多层目录)
def mkdirNotExist(directory):
# 防止创建文件目录时乱码
directory = directory.decode('utf-8')
if not os.path.exists(directory):
os.makedirs(directory)