更新时间:2023-09-25 22:10:58
使用 os.walk().下面将输出dir"子目录中所有文件的列表.结果可以根据您的需要进行处理:
Use os.walk(). The following will output a list of all files within the subdirectories of "dir". The results can be manipulated to suit you needs:
import os
def list_files(dir):
r = []
subdirs = [x[0] for x in os.walk(dir)]
for subdir in subdirs:
files = os.walk(subdir).next()[2]
if (len(files) > 0):
for file in files:
r.append(os.path.join(subdir, file))
return r
对于 python 3,将 next()
更改为 __next__()
.
For python 3, change next()
to __next__()
.