利用 os.walk 函数读取不同文件夹里的数据
在很多情况下,我们需要读取某个文件夹中不同子文件夹里的各种数据文件. 此时, 我们可以利用 os.walk() 进行迭代执行.
os.walk() 返回一个迭代器, 包括根目录(roots)、子目录(dirs)和文件(files)三个内容:
- roots 就是所有文件夹名作为根目录
- dirs 就是该文件夹下的子文件夹名
- files 路径下所有的文件名
实现
import os
path = r'/Users/chunzhongli/JupyterProjects/ORL_faces'
for root, dirs, files in os.walk(path):
print('\n----------root----------')
print(root)
print('----------dir----------')
for name in dirs:
file_name = os.path.join(root,name)
print(file_name)
print('-----------file------------')
for name in files:
file_name = os.path.join(root, name)
print(file_name)
----------root----------
/Users/chunzhongli/JupyterProjects/ORL_faces
----------dir----------
/Users/chunzhongli/JupyterProjects/ORL_faces/s34
/Users/chunzhongli/JupyterProjects/ORL_faces/s33
/Users/chunzhongli/JupyterProjects/ORL_faces/s5
/Users/chunzhongli/JupyterProjects/ORL_faces/s2
/Users/chunzhongli/JupyterProjects/ORL_faces/s32
/Users/chunzhongli/JupyterProjects/ORL_faces/s35
/Users/chunzhongli/JupyterProjects/ORL_faces/s3
/Users/chunzhongli/JupyterProjects/ORL_faces/s4
/Users/chunzhongli/JupyterProjects/ORL_faces/s26
/Users/chunzhongli/JupyterProjects/ORL_faces/s19
/Users/chunzhongli/JupyterProjects/ORL_faces/s21
/Users/chunzhongli/JupyterProjects/ORL_faces/s17
/Users/chunzhongli/JupyterProjects/ORL_faces/s28
/Users/chunzhongli/JupyterProjects/ORL_faces/s10
/Users/chunzhongli/JupyterProjects/ORL_faces/s11
/Users/chunzhongli/JupyterProjects/ORL_faces/s16
/Users/chunzhongli/JupyterProjects/ORL_faces/s29
/Users/chunzhongli/JupyterProjects/ORL_faces/s20
/Users/chunzhongli/JupyterProjects/ORL_faces/s27
/Users/chunzhongli/JupyterProjects/ORL_faces/s18
/Users/chunzhongli/JupyterProjects/ORL_faces/s8
/Users/chunzhongli/JupyterProjects/ORL_faces/s1
/Users/chunzhongli/JupyterProjects/ORL_faces/s6
/Users/chunzhongli/JupyterProjects/ORL_faces/s39
/Users/chunzhongli/JupyterProjects/ORL_faces/s30
/Users/chunzhongli/JupyterProjects/ORL_faces/s37
/Users/chunzhongli/JupyterProjects/ORL_faces/s7
/Users/chunzhongli/JupyterProjects/ORL_faces/s9
/Users/chunzhongli/JupyterProjects/ORL_faces/s36
/Users/chunzhongli/JupyterProjects/ORL_faces/s31
/Users/chunzhongli/JupyterProjects/ORL_faces/s38
/Users/chunzhongli/JupyterProjects/ORL_faces/s22
/Users/chunzhongli/JupyterProjects/ORL_faces/s25
/Users/chunzhongli/JupyterProjects/ORL_faces/s13
/Users/chunzhongli/JupyterProjects/ORL_faces/s14
/Users/chunzhongli/JupyterProjects/ORL_faces/s40
/Users/chunzhongli/JupyterProjects/ORL_faces/s15
/Users/chunzhongli/JupyterProjects/ORL_faces/s12
/Users/chunzhongli/JupyterProjects/ORL_faces/s24
/Users/chunzhongli/JupyterProjects/ORL_faces/s23
-----------file------------
/Users/chunzhongli/JupyterProjects/ORL_faces/.DS_Store
/Users/chunzhongli/JupyterProjects/ORL_faces/README
----------root----------
/Users/chunzhongli/JupyterProjects/ORL_faces/s34
----------dir----------
-----------file------------
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/7.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/6.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/4.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/5.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/1.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/2.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/3.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/8.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/9.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s34/10.pgm
----------root----------
/Users/chunzhongli/JupyterProjects/ORL_faces/s33
----------dir----------
-----------file------------
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/7.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/6.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/4.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/5.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/1.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/2.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/3.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/8.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/9.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s33/10.pgm
----------root----------
/Users/chunzhongli/JupyterProjects/ORL_faces/s5
----------dir----------
-----------file------------
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/7.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/6.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/4.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/5.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/1.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/2.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/3.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/8.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/9.pgm
/Users/chunzhongli/JupyterProjects/ORL_faces/s5/10.pgm