-1

I am trying to make a code in Python where when it runs it will search a directory and its sub-directories for files ending with a file extension ".pdm". I want to note this is not on a personal computer but on a cloud provider. The variable current_dur is just a starting point to narrow down the search. The directory has to go at through at least 3 more folders in its sub-directories.

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)

It returns the error message [WinError 3] The system cannot find the path specified: 'Archive'

Dan Nagle
  • 4,384
  • 1
  • 16
  • 28
  • 1
    What is your question? – Scott Hunter Jun 25 '21 at 13:10
  • I edited it. But my question is how do I navigate to other directories in Python. I want to automate this because I am looking for 200 .pdm files that are dispersed in over 20 different directories. – Steven Marsh Jun 25 '21 at 13:22
  • 1
    See [os.walk](https://docs.python.org/3/library/os.html#os.walk) –  Jun 25 '21 at 13:23
  • I think your issue is that "current_dur" is not a valid path. Try to display your current location os.path.abspath("./"), then try to move from one directory from another until you find where the path is broken. – Florian Fasmeyer Jun 25 '21 at 13:24

1 Answers1

2

Have you tried using 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))
Dan Nagle
  • 4,384
  • 1
  • 16
  • 28