os.walk() 返回的是generator
os.listdir() 返回的是list
import os
file_dir = r"D:\\code\\1"
i = 1
a = os.walk(file_dir)
b = None
for root, dirs, files in os.walk(file_dir):
print(i)
i += 1
print(root) #当前目录路径
print(dirs) #当前路径下所有子目录
print(files) #当前路径下所有非目录子文件
print(b)
会递归将目录下每个文件夹的内容都输出
输出
1
D:\code\1
[‘123’, ‘234’]
[‘flash_back_binlog.000000002’, ‘flash_back_binlog.000000003’, ‘flash_back_binlog.000001’]
2
D:\code\1\123
[‘123_1’]
[‘flash_back_binlog.000000001’, ‘flash_back_binlog.000000002’]
3
D:\code\1\123\123_1
[]
[‘flash_back_binlog.000000001’, ‘flash_back_binlog.000000002’]
4
D:\code\1\234
[‘234_1’]
[‘flash_back_binlog.000000001’, ‘flash_back_binlog.000000002’]
5
D:\code\1\234\234_1
[]
[‘flash_back_binlog.000000001’, ‘flash_back_binlog.000000002’]
None
import os
path = r"D:\\code\\1"
print(os.listdir(path))
只会将当前层的文件盒文件夹输出
输出
[‘123’, ‘234’, ‘flash_back_binlog.000000002’, ‘flash_back_binlog.000000003’, ‘flash_back_binlog.000001’]