获取包含文件的子文件夹路径的最有效方法是什么。例如,如果这是我的输入结构
inputFolder
│
└───subFolder1
│ │
│ └───subfolder11
│ │ file1.jpg
│ │ file2.jpg
│ │ ...
│
└───folder2
│ file021.jpg
│ file022.jpg
如果我通过getFolders(inputPath)
,
它应该以包含图像的文件夹列表的形式返回输出['inputFolder/subFolder1/subFolder11','inputFolder/folder2']
目前,我正在使用我的库TreeHandler,它只是os.walk
的包装器来获取所有文件
import os
from treeHandler import treeHandler
th=treeHandler()
tempImageList=th.getFiles(path,['jpg'])
### basically tempImageList will be list of path of all files with '.jpg' extension
### now is the filtering part,the line which requires optimisation.
subFolderList=list(set(list(map(lambda x:os.path.join(*x.split('/')[:-1]),tempImageList))))
我认为这样做可以更有效率
提前谢谢
查找最后一个“/”实例的索引和切片的速度要快得多
已在AWA2数据集文件夹(50个文件夹和37322个图像)上验证
添加用于验证的代码
相关问题 更多 >
编程相关推荐