-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist_file.py
135 lines (123 loc) · 5.13 KB
/
list_file.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#。——————————————————————————————————————————
#。
#。 list_file.py
#。
#。 @Time : 2018/7/26 00:09
#。 @Author : capton
#。 @Software: PyCharm
#。 @Blog : http://ccapton.cn
#。 @Github : https://github.com/ccapton
#。 @Email : [email protected]
#。__________________________________________
import os,time
# 文件、文件夹寻找类 (阻塞型)
# 阻塞的设计: 为了等待调用者的耗时操作【否则很快就完成了文件的遍历任务,调用者达不到顺序操作文件(夹)的意图】
#
from language_words import languageSelecter
class FileFinder:
def __init__(self,finderCallback):
self.finderCallback = finderCallback
# 文件(夹)路径下所有文件的总大小
self.sum_size = 0
# 调用者控制的参数,若为False,则遍历工作继续进行,若为True,则阻塞任务,等待调用者完成它的其他耗时操作后在考虑是否改变此值
self.recycle = True
# 调用者控制的参数,若为False,则正常工作,若为True,则当recycle为False时遍历工作不阻塞快速完成,recycle为True时遍历工作阻塞
self.off = False
# 文件(夹)找到时的回调类
class FinderCallback:
# 找到文件夹
def onFindDir(self,dir_path):
pass
# 找到文件
def onFindFile(self,file_path,size):
pass
# 预留的刷新函数
def onRefresh(self):
pass
# 查找文件(夹)方法
def list_flie(self,root_dir):
if os.path.isfile(root_dir):
while self.recycle:
time.sleep(0.05)
if self.finderCallback:
self.finderCallback.onFindFile(root_dir,os.path.getsize(root_dir))
self.finderCallback.onRefresh()
if not self.off:
self.recycle = True
else:
dirlist = os.listdir(root_dir) # 列出文件夹下所有的目录与文件
for dir in dirlist:
path = os.path.join(root_dir, dir)
if os.path.isfile(path):
while self.recycle:
time.sleep(0.05)
if self.finderCallback:
self.finderCallback.onFindFile(path,os.path.getsize(path))
self.finderCallback.onRefresh()
if not self.off:
self.recycle = True
else:
while self.recycle:
time.sleep(0.05)
if self.finderCallback:
self.finderCallback.onFindDir(path)
self.finderCallback.onRefresh()
if not self.off:
self.recycle = True
# 递归调用(当遍历到文件夹时,继续遍历,直到当前文件夹下没有文件夹为止)
self.list_flie(path)
def dict(key):
return languageSelecter.dict(key)
# 文件、文件夹寻找类2(快速寻找,无阻塞)
class FileFinder_Fast:
def __init__(self,finderCallback):
self.finderCallback = finderCallback
self.debug = True
# 文件(夹)路径下所有文件的总大小
self.sum_size = 0
# 文件(夹)找到时的回调类
class FinderCallback:
# 找到文件夹
def onFindDir(self,dir_path):
pass
# 找到文件
def onFindFile(self,file_path,size):
pass
# 预留的刷新函数
def onRefresh(self):
pass
def list_flie(self,root_dir):
if os.path.isfile(root_dir):
if self.debug:print('%s%s' % (dict('ff'),root_dir))
if self.finderCallback:
self.finderCallback.onFindFile(root_dir,os.path.getsize(root_dir))
self.finderCallback.onRefresh()
else:
dirlist = os.listdir(root_dir)
for dir in dirlist:
path = os.path.join(root_dir, dir)
if os.path.isfile(path):
if self.debug:print('%s%s' % (dict('ff'),path))
if self.finderCallback:
self.finderCallback.onFindFile(path,os.path.getsize(path))
self.finderCallback.onRefresh()
else:
if self.finderCallback:
self.finderCallback.onFindDir(path)
self.finderCallback.onRefresh()
if self.debug:print('%s%s' % (dict('fd'),path))
self.list_flie(path)
class MyfinderCallback(FileFinder.FinderCallback):
def onFindDir(self,dir_path):
print(dir_path)
def onFindFile(self,file_path,size):
print(' ' + file_path + ' ' + str(size))
if __name__ == '__main__':
list = os.listdir('/Users/capton/desktop/bilibili')
for item in list:
print(os.path.join('/Users/capton/desktop/bilibili', item))
# finder = FileFinder(MyfinderCallback())
# finder.recycle = False
# finder.list_flie('/Users/capton/desktop/video_downloader')