One-liner to get flat list of files from a list of possible directories?

directory, list, python, python-2.7

Solution

You can chain the items into a single list:

from itertools import chain as ch


files = list(ch.from_iterable(os.listdir(x) for x in ['dir1', 'dir', 'dir3'] if os.path.isdir(x)))

shortest I can make it:

from itertools import chain as ch, ifilter as ifil, imap 
from os import path, listdir

files = list(ch.from_iterable(imap(listdir, ifil(path.isdir, ('dir1', 'dir', 'dir3')))))

If you just want to use the names then just iterate over the chain object.

Problem

Suppose we have a list of directories, some of which might not exist: ``` dirs = ['dir1','dir2','dir3'] ``` For sake of argument only two exist and their content is: ``` dir1 --file1a --file1b dir2 --file2a --file2b ``` What is the best one-liner to get a flat list of all the files? The closest I got was: ``` import os files = [ os.listdir(x) for x in ['dir1','dir','dir3'] if os.path.isdir(x) ] ``` But that gives me a nested list: ``` [['file1a','file1b'],['file2a','file2b']] ``` What one-liner do I have to use instead, if I want that to be `['file1a', 'file1b', 'file2a', 'file2b']`?

Original source