20221207
We can use os.listdir()
to list all files and folders under one root folder. When we want to list all subfolders and sub files under one root folder, os.listdir()
doesn't meet the requirement, and we need to use os.walk()
.
import os
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
root: a current folder that is under search. Not the top folder where we want to list all subfolders and all subfiles. Only the folder that the code is currently searching.
dirs: a list containing all folders.
files: a list containing all files.
Reference:
[1] https://www.runoob.com/python/os-walk.html
[2] https://blog.csdn.net/lilong117194/article/details/74503143