0

I'm wanting to walk through a directory and search for a specific folder (knowing the name=="bar") and then recursively process all files. Note,do not process the directory "notbar". I can't seem to get the logic for finding the specific directory I need and not include the other duplicate files.

Given a general base_path = r'C:\data\foo'

How do I find "bar" and recursively walk through to process files.dat?

    C:\data\foo
       - notbar
           -123
             -file1.dat
           -456
             -file2.dat
             -file3.dat
       - bar
           -123
             -file1.dat
           -456
             -file2.dat
             -file3.dat
rdebruyn
  • 54
  • 8
  • 1
    have a look at glob... from glob import glob matched_files = sorted(glob(base_path + r"\bar\\*\file*.dat")) – geometrikal Sep 30 '20 at 04:12
  • I don't think the duplicate answer provided is the same issue . I understand how and ability to use os.walk but not how to find and only process files in the specified directory. If dir="bar" then process files.dat. but i'm probably missing something simple – rdebruyn Sep 30 '20 at 14:28
  • Yes I had a well written answer but the question was closed so quickly I didn’t get a chance to post! – geometrikal Sep 30 '20 at 14:31
  • @geometrikal thanks I asked a new question... maybe you can post there. [link](https://stackoverflow.com/questions/64139977/using-python-os-walk-how-to-check-for-directory-name-and-only-process-those-file) – rdebruyn Sep 30 '20 at 14:42

0 Answers0