Search code examples
pythondirectorypython-oslistdir

Automated way to list files from multiple directories in Python


I need to create a list holding all files from multiple directories.

I have all_dir which contains dir1, dir2, dir3.... Each directory contains multiple files ['text1.txt','text2.txt'...]. While I'm capable of creating list of single directories, I can't find the way to automate.

This is what I have and it work for the single directory.

path = '../all_dir'
list1 = [f for f in os.listdir(os.path.join(path, 'dir1')
list2 = [f for f in os.listdir(os.path.join(path,'dir1') 
#etc...

This would be the code I'm thinking of:

all_list = []

for dir1 in os.listdir(path):
    current = os.listdir(os.path.join(path,dir1))
    all_list.append(current)

But this for loop raise: NotADirectoryError

To fix this I've tried

all_list = []

for dir1 in os.listdir(path):
    current = os.walk(os.path.join(path,dir1))
    all_list.append(current)

But this loop raises a list of <generator object walk at 0x100ca4e40>

Could you help?


Solution

  • listdir also gives back files, so in the for loop you should do a check, if it is directory. You can use os.path.isdir()

    for dir1 in os.listdir(path):
        full_path = os.path.join(path, dir1)
        if os.path.isdir(full_path):
            current = os.listdir(full_path)
            all_list += current