我有一个文件夹结构
>root
> foldername1
> subfoldername1
> subfoldername2
> subfoldername3
> foldername2
> subfoldername1
> subfoldername2
> subfoldername3
> foldername3
> subfoldername1
> subfoldername2
> subfoldername3
我想列出'foldername2'的'subfoldername3'中包含的所有文件
我接下来的问题如下,但由于我有大量的文件夹,子文件夹和文件,它需要永远得出结论....是否有最快的方法来做同样的事情?
all_folders = [x[0] for x in walk(root)]
sub_folder = [s for s in all_folders if 'foldername2' in s]
matching_sub_path = [s for s in sub_folder if 'subfoldername3' in s]
matching_sub_path =
//root//foldername2//subfoldername3
我认为os.walk将递归列出所有目录。
为你的情况尝试这个,如果它适合你的需要。
all_folders = [x[0] for x in walk(root)]
all_files = [os.listdir(dir_path)
for dir_pathin all_folder
if "subfoldername3" and "foldername2" in dir_path]