[Python-ideas] Add OS-dependent automatic glob support

Steven D'Aprano steve at pearwood.info
Tue Jan 6 01:14:09 CET 2015


On Tue, Jan 06, 2015 at 03:09:39AM +0900, Stephen J. Turnbull wrote:
> Steven D'Aprano writes:
> 
>  > The model I am proposing is that of the os and os.path modules: we 
>  > import os and it does the right thing for the current platform.
> 
> Which is normally "nothing" on POSIX, since the shell does it for
> you.  Or, if you're talking about what the shell does for you, while I
> suppose there is a basic globbing defined by POSIX, but bash and zsh
> go well beyond that, and they behave somewhat differently in corner
> cases IIRC.

All correct.

> If the program is going to invoke a globbing function specifically, I
> suppose it makes sense to default to the same behavior as on the
> current platform, but it's not obvious that that's what the user
> wants.  I use bash on Windows; why settle for cmd.exe if you want the
> power of the command line?  It could even cause problems (is there a
> reliable way to determine what the shell is, let alone whether it
> implements globbing or not, on Windows?)

I see that as an application issue, not a Python issue. As the author of 
the application, I have some flexibility:

- Currently, I can not use glob.py at all, and my Unix users will be 
  able to use globs, but most of my Windows users won't.

- If I unconditionally use glob.py, my Unix users can not reliably 
  specify filenames containing wildcards but my Windows users may
  (assuming that glob supports escaping, which it currently doesn't).

- I can conditionally use glob or not, depending on the platform.

- Or I can provide an application switch/preference which allows the
  user to decide whether to use wildcards or not.


I don't know of any reliable way for a Python script to determine the 
current shell (there may not even be a shell!). If such a way exists, 
that would be good functionality to have.


>  > In particular, os.path will do the right thing for the current
>  > platform, but you can import ntpath, posixpath, genericpath etc. to
>  > get behaviour for a specific platform.
> 
> But there *is* a RightThang for paths; the OS decides.  Globbing is a
> user preference thing.

Not entirely user-preference. As Random's discussion has pointed out, 
there are issues with the way globbing is currently handled. For 
example, Windows doesn't support [...] wildcards and so most Windows 
users will expect to be able to specify [ and ] as ordinary characters. 
You can't do that with glob.py. Windows users who have bash installed 
are a very small minority :-)


-- 
Steven


More information about the Python-ideas mailing list