[clug] Python Golf time

Carlo Hamalainen carlo at carlo-hamalainen.net
Fri Apr 8 03:38:42 UTC 2016


Hi Andrew,

By first you mean the first file encountered in a breadth-first search of the directories below some root?

If so, breadth first search using the first result from os.path.walk with topdown=True will do the trick.


On 8 April 2016 9:25:01 am SGT, Andrew Janke <a.janke at gmail.com> wrote:
>Yes, Python.  I'm being forced off Perl.
>
>
>Shortest way to find the first file in a directory tree.
>
>Bonus points for finding a file of a specific mime type.  Everything
>I've tried uses os.walk or is like this:
>
>    for f in os.listdir(directory):
>        if f.endswith(".txt"):
>            winner = f
>            break
>
>(the above of course only deals with one directory level)
>
>
>a
>
>-- 
>linux mailing list
>linux at lists.samba.org
>https://lists.samba.org/mailman/listinfo/linux


More information about the linux mailing list