目录中的快速文件夹搜索 [Python]

发布于 2025-01-09 14:16:05 字数 1501 浏览 1 评论 0原文

情况:主目录中有很多文件和文件夹,非常旧的工作计算机,旧的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光 2025-01-16 14:16:05

你尝试过 os.walk 吗?您可以这样使用 os.walk:

def FindFolder(Path, WantedFolder):
    corresponding = []
    for root, _, _ in os.walk(Path):
        
        if root.split("\\")[-1] == WantedFolder:
            corresponding.append(root)

    return corresponding

您可能必须根据系统的架构来进行分割。该解决方案对于 odl 系统来说应该非常有效。

Have you tried os.walk? You can use os.walk as so:

def FindFolder(Path, WantedFolder):
    corresponding = []
    for root, _, _ in os.walk(Path):
        
        if root.split("\\")[-1] == WantedFolder:
            corresponding.append(root)

    return corresponding

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文