1

I need help getting a list of file names and locations within specific sub-directories. My directory is structured as follows:

C:\folder
-->\2014
----->\14-0023
-------->\(folders with files inside)
----->\CLOSED
-------->\14-0055!
----------->\(folders with files inside)
-->\2015
----->\15-0025
-------->\(folders with files inside)
----->\CLOSED
-------->\15-0017!
----------->\(folders with files inside)

I would like to get a list of files and their paths ONLY if they are within CLOSED.

I have tried writing multiple scripts and search questions on SO, but have not been able to come up with something to retrieve the list I want. While there seems to be questions related to my trouble, such as Filtering os.walk() dirs and files , they don't quite have the same requirements as I do and I've thus far failed to adapt code I've found on SO for my purpose.

For example, here's some sample code from another SO thread I found that I tried to adapt for my purpose.

l=[]
include_prefixes = ['CLOSED']
for dir, dirs, files in os.walk(path2, topdown=True):
    dirs[:] = [d for d in dirs if d in include_prefixes]
    for file in files:
        l.append(os.path.join(dir,file))

^the above got me an empty list...

After a few more failures, I thought to just get a list of the correct folder paths and make another script to iterate within THOSE folders.

l=[]
regex = re.compile(r'\d{2}-\d{4}', re.IGNORECASE)
for root, subFolders, files in os.walk(path2):
    try:
        subFolders.remove(regex)
    except ValueError:
        pass
    for subFolder in subFolders:
        l.append(os.path.join(root,subFolder))

^Yet I still failed and just got all the file paths in the directory. No matter what I do, I can't seem to force os.walk to (a) remove specific subdirs from it's list of subdirs and then (b) make it loop through those subdirs to get the file names and paths I need.

What should I fix in my example code? Or is there entirely different code that I should consider?

dart852
  • 65
  • 7

0 Answers0