如何自动 Python 导航到其他目录

How do I automate Python to navigate to other directories

我正在尝试在 Python 中编写一个代码,当它运行时,它将在一个目录及其子目录中搜索以文件扩展名“.pdm”结尾的文件。我想指出这不是在个人计算机上,而是在云提供商上。变量 current_dur 只是缩小搜索范围的起点。该目录必须在其子目录中再经过至少 3 个文件夹。

import os    
current_dur = r'\dmn1.MIR.com\MIRFILE\FS159\FIRSCODB\IR Data Modeling\PIR\IR - Information Report'

for item in os.listdir(current_dur):
    if not os.path.isfile(item):
        for file in os.listdir(item):
            if file.endswith('.pdm'):
                print(file)

它returns错误信息[WinError 3] The system cannot find the path specified: 'Archive'

您尝试过使用 os.walk() 吗?

import os    

current_dur = r'\dmn1.MIR.com\MIRFILE\FS159\FIRSCODB\IR Data Modeling\PIR\IR - Information Report'

pdm_files = []

for root, dirs, files in os.walk(current_dur):
    for file in files:
        if file.endswith('.pdm'):
            pdm_files.append(os.path.join(root, file))