[clug] Python Golf time

Hal Ashburner hal.ashburner at gmail.com
Fri Apr 8 03:24:36 UTC 2016


apologies. that's current directory

import glob
winner = glob.glob(directory + '/*.txt')[0]


Or if you want to be verbose about it
import os,glob
winner = glob.glob(os.path.join(directory, '*.txt'))[0]


On 8 April 2016 at 13:20, Andrew Janke <a.janke at gmail.com> wrote:
> Unless we've somehow switched to Haskell (that seems to know whatever
> you dont want it to do and heads down that path irrespective -- hi
> Carlo!) I think you need an input directory in there somewhere?
>
>
> a
>
> On 8 April 2016 at 13:18, Hal Ashburner <hal at ashburner.info> wrote:
>> import glob
>> winner = glob.glob('*.txt')[0]
>>
>> On 8 April 2016 at 11:25, 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