0

Hello users of stackoverflow,

I had a question on how to target specific folders when looking within a folder for example, look at the directory tree below:

--> MAIN FOLDER --> Subject 1 --> A_Folder --> files
                --> Subject 2 --> A_Folder --> files
                              --> B_Folder --> files
                --> Subject 3 --> A_Folder --> files
                              --> B_Folder --> files

So Subject 1 has only one subfolder, A_Folder, but Subject 2 and Subject 3 both have two sub folders, A_Folder and B_Folder.

My goal is I want to add the contents of Subject 2 and Subject 3's A_Folder and B_Folder, but ignore Subject 1's A_Folder.

Currently this is how I am targeting the folders...

A_files = []                                                                                                                                                               
for dirName, subdirList, fileList in os.walk(Path):                                                                                                                  
    for filename in fileList:                                                                                                                          
        if "A" in dirName:   
            A_files.append(os.path.join(dirName,filename))                                                                                                                                                                     

B_files = []                                                                                                      
for dirName, subdirList, fileList in os.walk(Path):                                                                                                              
    for filename in fileList:                                                                                                                          
        if "B" in dirName:                                                                                    
            B_files.append(os.path.join(dirName,filename))  

I appreciate your help!

Tyler_Cork
  • 19
  • 1
  • 3

1 Answers1

0

From the documentation:

the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search".

Code like this will walk through the directory structure, totally ignoring any "Subject 1" directory.

#UNTESTED
A_files = []                                                                                                                                                               
for dirName, subdirList, fileList in os.walk(Path):                                                                                                                  
    for filename in fileList:                                                                                                                          
        if "A" in dirName:   
            A_files.append(os.path.join(dirName,filename))
        if "B" in dirName:
            B_files.append(os.path.join(dirName,filename))
    if 'Subject 1' in subdirList:
        subdirList.remove('Subject 1')
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Rob, thank you for your advise, I thought I might need to use an if statement but I am looking for a dynamic way to code this for thousands of folders. Will something like this potentially work? – Tyler_Cork Jan 07 '16 at 19:03
  • A_files = [] for dirName, subdirList, fileList in os.walk(Path): for filename in fileList: if "A" && "B"in dirName: A_files.append(os.path.join(dirName,filename)) – Tyler_Cork Jan 07 '16 at 19:03
  • B_files.append(os.path.join(dirName,filename)) – Tyler_Cork Jan 07 '16 at 19:03