Ways to filter files in directory and join directory path - Python
<p>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.</p>

<p>Currently, I'm doing it as such:</p>

<pre><code>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)
</code></pre>

<p>I could also do it with <code>glob</code>:</p>

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

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

<p>Are there other ways of achieving the same filtered list of full paths to the files?</p>

<p>You can use a <a href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions" rel="nofollow"><code>list comprehension</code></a> to build the result in one go:</p>

<pre><code>&gt;&gt;&gt; [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']
</code></pre>

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

<p>For a large directory, it may be wise to use <a href="https://docs.python.org/3/library/os.html" rel="nofollow"><code>os.scandir</code></a> which returns an iterator. This will be way faster.</p>

<pre><code>&gt;&gt;&gt;Â [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']
</code></pre>

python operating-system filepath glob listdir