0

I had a question regarding how to list only the folders, not the files within the folders, in Python. I currently have this few lines of code which will join every file in ever folder into one list. If I have 50 folders, I want to make 50 different lists instead of one large one.

Folder_files = []                                                        
for dirName, subdirList, fileList in os.walk(Path):                                                                                                               
    for filename in fileList:                                                                                                                         
        if "A_0" in dirName:                                                                                  
            if ".xml" in filename.lower():                                                       
                Folder_files.append(os.path.join(dirName,filename)) 

I know I have Folder_files as my only list at the moment, however I expect to experiment by using a for loop to have different lists.

I am just in need of help finding out how to read up to the folder A_0 before selecting the .xml files.

Before appending the files to the list. I attached a sample code of what I am thinking it will look like below:

    for dirName, subdirList, fileList in os.walk(Path):                                                                                                               
        for filename in fileList:                                                                                                                         
            if "A_0" in dirName: 
                #for loop in here
                    #Set up all folders                                       
                        if ".xml" in filename.lower():                                                       
                           #Add files in folder to the appropriate file list
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Tyler_Cork
  • 19
  • 1
  • 3

2 Answers2

0

It`s much better to use dict of lists for this purpose:

dirs = dict()
for dirName, subdirList, fileList in os.walk(root):
    if "A_0" in dirName:
        dir_list = dirName.split('/')[-1]   # if your directory path is '/foo/bar/baz' your list will be named 'baz'
        dirs[dir_list] = [os.path.join(dirName,filename) for filename in fileList if ".xml" in filename]

But you can use exec() if you really want to store each list as separate variable:

for dirName, subdirList, fileList in os.walk(root):
    if "A_0" in dirName:
        dir_list = dirName.split('/')[-1]   # if your directory path is '/foo/bar/baz' your list will be named 'baz'
        exec(dir_list + '= [os.path.join(dirName,filename) for filename in fileList if ".xml" in filename]')

It is much harder to understand and maintain the second piece of code. So I encourage you to use dictionary of lists.

0
import os

dirs= next(os.walk(r'/home/sirmagid/public_html/ts/'))[1]
for name in dirs:
    print name
sirmagid
  • 1,090
  • 1
  • 16
  • 23