Using glob (was Re: Remove files of same type (extension))

Peter Hansen peter at engcorp.com
Wed Sep 26 09:53:38 EDT 2001


Boyd Roberts wrote:
> 
> "Oleg Broytmann" <phd at phd.pp.ru> a écrit dans le message news: mailman.1001409765.10101.python-list at python.org...
> >    This has nothing with ls. The * expansion is the shell's job. Example:
> >
> > echo *
> > echo .*
> 
> ls
> ls -a
> 
> QED

'ls' lists something for every name listed, unless you list no names.
The -a option is something completely outside of the shell globbing,
as near as I can tell from the following.  It appears to make ls
do its own dir list and return (in addition to anything else it was
asked to return) the files starting with a period.

  [phansen]$ ls
  test.py
  [phansen]$ ls -a
  .  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  test.py
  [phansen]$ ls .*
  .bash_history  .bash_logout  .bash_profile  .bashrc

  .:
  test.py

  ..:
  phansen

  [phansen]$ ls -a .*
  .bash_history  .bash_logout  .bash_profile  .bashrc

  .:
  .  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  test.py

  ..:
  .  ..  phansen

With the exception that .* returns . and .. under Linux but not
under glob.glob(), the following answers the question nicely for me:

  [phansen]$ echo *
  test.py
  [phansen]$ echo .*
  . .. .bash_history .bash_logout .bash_profile .bashrc

  [phansen]$ python
  Python 1.5.2 (#1, Mar  3 2001, 01:35:43)  [GCC 2.96 20000731 (Red Hat Linux 7.1 2 on linux-i386
  Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
  >>> import glob
  >>> glob.glob('*')
  ['test.py']
  >>> glob.glob('.*')
  ['.bash_logout', '.bash_profile', '.bashrc', '.bash_history']

QED?  Actually, I don't know what you were trying to prove, exactly.
Oleg's point, which appears valid, is that whatever ls is up to is
independent of the shell's filename expansion, which is expressly 
what glob.glob() is emulating.  Since one could ignore ls entirely
and still have lots of instances of globbing going on, I don't 
understand the part about it being only "with the complicity of ls".

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list