0

Given a suffix and a directory path, I need to extract the full path of the files in the directory that ends with a given suffix.

Currently, I'm doing it as such:

import os
dir_path = '/path/to/dir'
suffix = '.xyz'
filenames = filter(lambda x: x.endswith(suffix), os.listdir(dir_path))
filenames = map(lambda x: os.path.join(dir_path, x), filenames)

I could also do it with glob:

import glob
dir_path = '/path/to/dir'
suffix = '.xyz'
glob.glob(dir_path+'*.'+suffix)

I understand that there's also pathlib that can check for suffixes using PurePath but I'm not sure what is the syntax for that.

Are there other ways of achieving the same filtered list of full paths to the files?

1 Answer 1

4

You can use a list comprehension to build the result in one go:

>>> [os.path.join(os.sep, x, dir_path) for x in os.listdir(dir_path) 
if x.endswith(suffix)]
['/home/msvalkon/foo.txt', 
 '/home/msvalkon/output.txt',
 '/home/msvalkon/remaining_warnings.txt',
 '/home/msvalkon/test.txt',
 '/home/msvalkon/hdr_chksum_failure.txt']

If dir_path is always an absolute path, you can use os.path.abspath(x) in place of the os.path.join().

For a large directory, it may be wise to use os.scandir which returns an iterator. This will be way faster.

>>> [entry.path for entry in os.scandir(dir_path) if entry.name.endswith(suffix)]
 ['/home/msvalkon/foo.txt', 
  '/home/msvalkon/output.txt',
  '/home/msvalkon/remaining_warnings.txt',
  '/home/msvalkon/test.txt',
  '/home/msvalkon/hdr_chksum_failure.txt']
Sign up to request clarification or add additional context in comments.

4 Comments

Short question, given 1,000,000 files in the directory, would filter + map be faster than list comprehension, or the reverse is true? Or would they be the same?
Given that filter() will create one list, and map() another, I would wager that the list comprehension is faster. Though, with such numbers, it may be wiser to find out a way to use a generator.
Actually, they won't be creating lists, both filter and map returns generators.
@alvas that's true for python 3, but not for python 2. In python 3 both return an iterator, in python2 both construct the resulting list. See my update with os.scandir().

Your Answer

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.