目录中的快速文件夹搜索 [Python]
情况:主目录中有很多文件和文件夹,非常旧的工作计算机,旧的 python 2.something(软件绑定,所以现在无需执行任何操作),所有数据都在网络驱动器上。
我有一个变量,其中包含它应该找到的文件夹名称,每个项目中有大约 2-4 个这样的文件夹名称。对于 ShotName = "DVX_010_400_BG_V001"
预期输出:
C:\Users\Dame\Desktop\NUKESTUFF\TPN\FilmLance\DVX\3dcomp\DVX_010_400_BG_V001
C:\Users\Dame\Desktop\NUKESTUFF\TPN\FilmLance\DVX\input\DVX_010_400_BG_V001
C:\Users\Dame\Desktop\NUKESTUFF\TPN\FilmLance\DVX\render\DVX_010_400_BG_V001
目标:找到与 ShotName 变量同名的文件夹并返回其路径
问题:当前代码太慢并且挂起。
在我的桌面环境中,主文件夹位于 dekstop(快速 NVME ssd)上,最快的处理器,现代 python,所以速度很快,大约 2 秒。我最好的代码:
import os
ShotName = "DVX_010_400_BG_V001"
head_dir= "C:\\Users\\Dame\\Desktop\\NUKESTUFF\\TPN\\"
def dir_list_folder(head_dir, ShotName):
"""Return a list of the full paths of the subdirectories
under directory 'head_dir' named 'ShotName'"""
dirList = []
for fn in os.listdir(head_dir):
dirfile = os.path.join(head_dir, fn)
if os.path.isdir(dirfile):
print (dirfile)
if fn.upper() == ShotName.upper():
dirList.append(dirfile)
else:
dirList += dir_list_folder(dirfile, ShotName)
return dirList
if __name__ == '__main__':
for item in dir_list_folder(head_dir, ShotName):
# print (item)
if "render" in item :
ProjectPath = item.split("\\")[:-2]
ProjectPath = "/".join(ProjectPath)
print (f"ProjectPath = {ProjectPath}")
Situation: A lot of files and folders into the main directory, very old work computers, old python 2.something(Software bound so nothing to be done for now), all the data is on a network drive.
I have a variable which contains the folder name it is supposed to find, there are like 2-4 of these withing each project. For ShotName = "DVX_010_400_BG_V001"
Expected output:
C:\Users\Dame\Desktop\NUKESTUFF\TPN\FilmLance\DVX\3dcomp\DVX_010_400_BG_V001
C:\Users\Dame\Desktop\NUKESTUFF\TPN\FilmLance\DVX\input\DVX_010_400_BG_V001
C:\Users\Dame\Desktop\NUKESTUFF\TPN\FilmLance\DVX\render\DVX_010_400_BG_V001
Goal: to find the folders that have the same name as the ShotName variable and return their path
Broblem: Current code is too slow and it hangs.
in my desktop env, the main folder is on dekstop (fast NVME ssd), the fastest processor, modern python so it's fast, like 2 seconds. My best code:
import os
ShotName = "DVX_010_400_BG_V001"
head_dir= "C:\\Users\\Dame\\Desktop\\NUKESTUFF\\TPN\\"
def dir_list_folder(head_dir, ShotName):
"""Return a list of the full paths of the subdirectories
under directory 'head_dir' named 'ShotName'"""
dirList = []
for fn in os.listdir(head_dir):
dirfile = os.path.join(head_dir, fn)
if os.path.isdir(dirfile):
print (dirfile)
if fn.upper() == ShotName.upper():
dirList.append(dirfile)
else:
dirList += dir_list_folder(dirfile, ShotName)
return dirList
if __name__ == '__main__':
for item in dir_list_folder(head_dir, ShotName):
# print (item)
if "render" in item :
ProjectPath = item.split("\\")[:-2]
ProjectPath = "/".join(ProjectPath)
print (f"ProjectPath = {ProjectPath}")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你尝试过 os.walk 吗?您可以这样使用 os.walk:
您可能必须根据系统的架构来进行分割。该解决方案对于 odl 系统来说应该非常有效。
Have you tried os.walk? You can use os.walk as so:
You will probably have to play with the split depending of the architecture of you system. This solution shoud be pretty efficient for odl systems.