[Python-ideas] Add OS-dependent automatic glob support
Georg Brandl
g.brandl at gmx.net
Tue Jan 6 00:40:19 CET 2015
On 01/05/2015 07:45 PM, Skip Montanaro wrote:
> Indeed. I only have 2.7.2 available here at work. Here's what bash tells me on a
> Linux box:
>
> % ls yen{2,3}.*
> yen2.out yen2.png yen2.tradeyen3.out yen3.png yen3.trade
> % ls yen[23].*
> yen2.out yen2.png yen2.tradeyen3.out yen3.png yen3.trade
[...]
> Here's what the glob module tells me:
>
> % python
> Python 2.7.2 (default, Nov 14 2012, 05:07:35)
> [GCC 4.4.6 [TWW]] on linux3
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import glob
>>>> glob.glob("yen{2,3}.*")
> []
>>>> glob.glob("yen[23].*")
> ['yen3.trade', 'yen2.out', 'yen2.trade', 'yen3.out', 'yen3.png', 'yen2.png']
>
> I only discovered this "shortcoming" (or "Bourne Shell dependency") relatively
> recently. I've been using bash for so long it never even occurred to me that
> {...} notation wasn't available in all shells.
Note that the {...} notation is not a part of globbing, it's a different
mechanism (bash calls it brace expansion IIRC). With brace expansion, the
different choices are *always* expanded regardless of the existence of
matching filenames. Your pattern first gets expanded to "yen2.* yen3.*" and
then globbing ensues (with the standard rule that if there is nothing matching
e.g. yen2.* it is either given literally to the program or the command line is
rejected, depending on the shell).
=> I wouldn't expect the Python glob module to perform brace expansion.
cheers,
Georg
More information about the Python-ideas
mailing list