[clug] Python Golf time

Scott Ferguson scott.ferguson.clug at gmail.com
Fri Apr 8 04:22:45 UTC 2016



On 08/04/16 11:25, Andrew Janke 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
> 


For the purposes of my education (python clueless), I'll bite (but not
at the specific filetype.


os.listdir(path)[0]


e.g. import os;path = '/home/scott/soe_selections';os.listdir(path)[0]
'aaron_package_selections'

Instantly returns a result from a directory with several thousand files


Constructive comments and criticisms welcomed





Kind regards


-- 
    A: Because we read from top to bottom, left to right.
    Q: Why should I start my reply below the quoted text?

    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?

    A: The lost context.
    Q: What makes top-posted replies harder to read than bottom-posted?

    A: Yes.
    Q: Should I trim down the quoted part of an email to which I'm reply

http://www.idallen.com/topposting.html



More information about the linux mailing list