listdir() with mask

Jeff McNeil jeff at jmcneil.net
Fri Dec 14 07:18:16 EST 2007


Sure is.. check out the glob module:
http://www.python.org/doc/current/lib/module-glob.html (Official)
http://blog.doughellmann.com/2007/07/pymotw-glob.html (PyMOTW)

Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import glob
>>> glob.glob("/var/log/*.log")
['/var/log/alf.log', '/var/log/asl.log', '/var/log/crashreporter.log',
'/var/log/fsck_hfs.log', '/var/log/ftp.log', '/var/log/install.log',
'/var/log/ipfw.log', '/var/log/lpr.log', '/var/log/mail.log',
'/var/log/mb.log', '/var/log/netinfo.log', '/var/log/ppp.log',
'/var/log/secure.log', '/var/log/system.log', '/var/log/windowserver.log',
'/var/log/windowserver_last.log']
>>> glob.glob("/var/*/*.log")
['/var/log/alf.log', '/var/log/asl.log', '/var/log/crashreporter.log',
'/var/log/fsck_hfs.log', '/var/log/ftp.log', '/var/log/install.log',
'/var/log/ipfw.log', '/var/log/lpr.log', '/var/log/mail.log',
'/var/log/mb.log', '/var/log/netinfo.log', '/var/log/ppp.log',
'/var/log/secure.log', '/var/log/system.log', '/var/log/windowserver.log',
'/var/log/windowserver_last.log']
>>>

-Jeff

On 12/14/07, Vladimir Rusinov <vladimir at greenmice.info> wrote:
>
> Hello!
>
> Is there any easy way to list files using bash-like patterns? Something
> like listfiles("/var/log/*.log"), listfiles("/var/{cache,run}/*").
>
> Also, I'll need something like listfiles("/tmp/**/*.tmp"), where ** is
> unlimited number of folders (like is zsh).
>
> Thanks and sorry for my English.
>
> --
> Vladimir Rusinov
> GreenMice Solutions: IT-решения на базе Linux
> http://greenmice.info/
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071214/85d621d6/attachment.html>


More information about the Python-list mailing list