forked from crisschan/emma_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_dirAndfiles.py
66 lines (56 loc) · 1.87 KB
/
get_dirAndfiles.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#__from__ = 'PythonSpace'
#__title__='PyCharm'
#__author__ = 'chancriss'
#__mtime__ = '2017/10/9'
#__instruction__='获取各种目录文件'
import os
class DirAndFiles(object):
def __init__(self):
self.__list=[]
def allFiles(self,sOrgDir): # 传入存储的list
'''
Args:
sOrgDir: 目录
Returns:
list:全部文件,包含子目录内的
'''
self.__list=[]
for file in os.listdir(sOrgDir):
file_path = os.path.join(sOrgDir, file)
if os.path.isdir(file_path):
self.files(file_path)
else:
self.__list.append(file_path)
return self.__list
def dirAndFiles(self,sOrgDir):
'''
Args:
sOrgDir: 目录
Returns:
list[dict]:dict{'root':root,'dirs':dirs,'files':files},root是根目录、dirs root下的第一级子目录、files root下第一级文件
'''
self.__list=[]
for root, dirs, files in os.walk(sOrgDir):
self.__list.append({'root':root,'dirs':dirs,'files':files})
return self.__list
def filesWithFilter(self,sOrgDir,sEx):
'''
Args:
sOrgDir: 目录
sEx: 需要查找的扩展名
Returns:
list:符合扩展名的全部文件,包含子目录里面的
'''
self.__list=[]
for root, dirs, files in os.walk(sOrgDir):
for file in files:
if os.path.splitext(file)[1] == sEx:
self.__list.append(os.path.join(root, file))
return self.__list
if __name__=='__main__':
df = DirAndFiles()
#print df.files('/Users/chancriss/Downloads')
#print df.dirfiles('/Users/chancriss/Downloads')
#print df.filesfilter('/Users/chancriss/Downloads','.jpg')