forked from deepindeed2022/pyrecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalk_subfiles.py
55 lines (51 loc) · 1.52 KB
/
walk_subfiles.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
#!/usr/bin/python
#-*-encoding=utf-8 -*-
"""
返回该路径下的所有文件
argv:
srcdir 输入文件的路径
return:
filelist该路径下的所有文件列表
"""
import os
import os.path
def get_subfiles(srcdir = None):
filelist= []
def process_dir(arg, dirname, names):
for name in names:
name_path = "{}{}{}".format(dirname, os.sep, name)
if os.path.islink(name_path):
os.path.walk(name_path, process_dir, arg)
elif os.path.isfile(name_path):
filelist.append(os.path.abspath(name_path))
if srcdir == None:
return []
if isinstance(srcdir, str):
os.path.walk(srcdir, process_dir, "")
return filelist
elif isinstance(srcdir, list):
for sdir in srcdir:
os.path.walk(sdir, process_dir, "")
return filelist
def list_dir(path, suffix ='.txt'):
l = os.listdir(path)
result = []
for line in l:
if line.endswith(suffix):
result.append(os.path.join(path, line))
return result
def test_get_subfiles():
srcdir = "testcase"
filelist = [
"./testcase/1.txt",
"./testcase/data.txt",
"./testcase/folder/1.txt",
"./testcase/folder/folder2/1.txt",
"./testcase/folder2/1.txt"]
filelist2 = [os.path.abspath(i) for i in filelist]
srcdir = os.path.abspath(srcdir)
l = get_subfiles(srcdir)
assert set(l) == set(filelist2)
if __name__ == '__main__':
test_get_subfiles()
print list_dir("testcase", '.txt')