Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: os.listdir alternative/certain extensions

Is it possible to see files with certain extensions with the os.listdir command? I want it to work so it may show only files or folders with .f at the end. I checked the documentation, and found nothing, so don't ask.

like image 745
Galilsnap Avatar asked Jun 26 '10 02:06

Galilsnap


1 Answers

Don't ask what?

[s for s in os.listdir() if s.endswith('.f')]

If you want to check a list of extensions, you could make the obvious generalization,

[s for s in os.listdir() if s.endswith('.f') or s.endswith('.c') or s.endswith('.z')]

or this other way is a little shorter to write:

[s for s in os.listdir() if s.rpartition('.')[2] in ('f','c','z')]
like image 111
David Z Avatar answered Sep 27 '22 19:09

David Z