4
\$\begingroup\$

Given a base path and a list with extensions the task is to list all files:

Two of my solutions are:

from glob import glob
from os import path
EXTENSIONS = ['*.zip', '*.jar', '*.pdf']
DOC_PATH = '/path/to/files'
# Solution1:
files = [] 
for ext in EXTENSIONS:
 files.extend(glob(path.join(DOC_PATH, ext)))
# works but looks very clumsy
# Solution2:
files = reduce(lambda x,y: x+y,
 [glob(path.join(DOC_PATH, ext)) for ext in EXTENSIONS])
# Also functional but looks like a misuse of reduce

Have you got any other ideas?

asked Jul 7, 2014 at 12:08
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Not quite sure how idiomatic/efficient it is : sum((glob(path.join(DOC_PATH, ext)) for ext in EXTENSIONS), []) . \$\endgroup\$ Commented Jul 7, 2014 at 12:20

2 Answers 2

3
\$\begingroup\$

If you only need to iterate over them (once) and not an actual list, you could use itertools.chain and glob.iglob:

files = chain(*(iglob(path.join(DOC_PATH, ext)) for ext in EXTENSIONS))

If you do need an actual list, you can further call list(files), of course.

answered Jul 7, 2014 at 12:30
\$\endgroup\$
1
  • \$\begingroup\$ chain is quite a good idea \$\endgroup\$ Commented Jul 7, 2014 at 12:35
3
\$\begingroup\$

Using os.listdir and os.path.splitext:

import os
EXTENSIONS = 'zip jar pdf'.split()
EXTENSION_SET = set('.' + e for e in EXTENSIONS)
files = [f for f in os.listdir(DOC_PATH) if os.path.splitext(f)[1] in EXTENSION_SET]

Using os.listdir and re.search:

import os
import re
EXTENSIONS = 'zip jar pdf'.split()
EXTENSION_RE = re.compile(r'\.({})$'.format('|'.join(EXTENSIONS))
files = [f for f in os.listdir(DOC_PATH) if EXTENSION_RE.search(f)]
# or files = list(filter(EXTENSION_RE.search, os.listdir(DOC_PATH)))

The advantage of these approaches is that you only iterate over the files in the directory once (rather than once for each extension, as in the glob case).

answered Jul 7, 2014 at 13:07
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.