Given a directory of files, e.g.:
mydir/
test1.abc
set123.abc
jaja98.abc
test1.xyz
set123.xyz
jaja98.xyz
I need to check that for every .abc
file there is an equivalent .xyz
file. I could do it like this:
>>> filenames = ['test1.abc', 'set123.abc', 'jaja98.abc', 'test1.xyz', 'set123.xyz', 'jaja98.xyz']
>>> suffixes = ('.abc', '.xyz')
>>> assert all( os.path.splitext(_filename)[0]+suffixes[1] in filenames for _filename in filenames if _filename.endswith(suffixes[0]) )
The above code should pass the assertion, while something like this would fail:
>>> filenames = ['test1.abc', 'set123.abc', 'jaja98.abc', 'test1.xyz', 'set123.xyz']
>>> suffixes = ('.abc', '.xyz') >>> 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 "<stdin>", line 1, in <module>
AssertionError
But that's a little too verbose.
Is there a better to do the same checks?