Check parallelism of files using their suffixes
<p>Given a directory of files, e.g.:</p>

<pre><code>mydir/
  test1.abc
  set123.abc
  jaja98.abc
  test1.xyz
  set123.xyz
  jaja98.xyz
</code></pre>

<p>I need to check that for every <code>.abc</code> file there is an equivalent <code>.xyz</code> file. I could do it like this:</p>

<pre><code>&gt;&gt;&gt; filenames = ['test1.abc', 'set123.abc', 'jaja98.abc', 'test1.xyz', 'set123.xyz', 'jaja98.xyz']
&gt;&gt;&gt; suffixes = ('.abc', '.xyz')
&gt;&gt;&gt; assert all( os.path.splitext(_filename)[0]+suffixes[1] in filenames for _filename in filenames if _filename.endswith(suffixes[0]) )
</code></pre>

<p>The above code should pass the assertion, while something like this would fail:</p>

<pre><code>&gt;&gt;&gt; filenames = ['test1.abc', 'set123.abc', 'jaja98.abc', 'test1.xyz', 'set123.xyz']
&gt;&gt;&gt; suffixes = ('.abc', '.xyz')                                                                                 &gt;&gt;&gt; assert all(os.path.splitext(_filename)[0]+suffixes[1] in filenames for _filename in filenames if _filename.endswith(suffixes[0]))
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AssertionError
</code></pre>

<p>But that's a little too verbose.<br>
Is there a better to do the same checks?</p>

<p>You could define helper function that would return <code>set</code> of filenames without extension that match to the given suffix. Then you could easily check is files with suffix <code>.abc</code> is subset of files with suffix <code>.xyz</code>:</p>

<pre><code>filenames = ['test1.abc', 'set123.abc', 'jaja98.abc', 'test1.xyz', 'set123.xyz', 'jaja98.xyz']
filenames2 = ['test1.abc', 'set123.abc', 'jaja98.abc', 'test1.xyz', 'set123.xyz']
suffixes = ('.abc', '.xyz')

def filter_ext(names, ext):
    return {n[:-len(ext)] for n in names if n.endswith(ext)}

assert filter_ext(filenames, suffixes[0]) &lt;= filter_ext(filenames, suffixes[1])
assert filter_ext(filenames2, suffixes[0]) &lt;= filter_ext(filenames2, suffixes[1]) # fail
</code></pre>

<p>Above approach would be more efficient as well since it has <strong>O(n)</strong> time complexity where as the original is <strong>O(n^2)</strong>. Of course if the list is small this doesn't really matter.</p>

python operating-system filepath file-extension suffix