-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython_os.py
55 lines (45 loc) · 1.54 KB
/
python_os.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
import os
import numpy as np
# 遍历文件 利用 os 的 walk 函数实现
level = []
def walkFile(file):
for root, dirs, files in os.walk(file):
# root 表示正在访问的文件夹路径
# dirs 表示该文件夹下的子目录名 list
# files 表示该文件夹下的文件 list
# print(os.walk(file))
# print(root)
# print(files)
#####按数字大小排列,而不是字符串大小
a = np.zeros(len(dirs))
for i in dirs:
a[dirs.index(i)] = float(i)
##### 冒泡排序
# for i in range(len(dirs)):
# for j in range(len(dirs) - i - 1):
# if a[j] > a[j + 1]:
# temp = a[j]
# a[j] = a[j + 1]
# a[j + 1] = temp
b = list(np.sort(a))
for i in b:
dirs[b.index(i)] = str(int(i))
###########################
###遍历文件 os.path.join(a,b) 是把 a 和 b 合为一个路径
# for f in files:
# print(os.path.join(root, f))
###遍历所有文件夹
level.append(dirs)
print(len(dirs), level)
for d in dirs:
# print(os.path.join(root, d))
a = os.listdir(os.path.join(root, d))
for i in a:
a[a.index(i)] = i[:-4]
def main():
walkFile('d:\\python\\tianzhen\\shixi2\\air')
if __name__ == '__main__':
main()
level_a = np.array(level)
print(len(level))
print(level[0])