Spawn/Exec with asterisk in argument

Michael Hoffman cam.ac.uk at mh391.invalid
Wed Apr 18 21:24:11 EDT 2007


jeremyfee at gmail.com wrote:
> The spawn* and exec* functions appear to escape asterisks, I'm
> guessing all shell characters too, before the spawn/exec'ed process
> sees them.

No. It is the shell that ordinarily processes these characters and gives 
them a special meaning. On most systems, ls does not give any special 
meaning to asterisk.

> Is there a way around this?

If you want to process asterisk the way the shell does, you can pass 
things through the shell. os.system is one way of doing that. Probably 
better is:

subprocess.check_call("ls -l *", shell=True)

Another way is using the glob module (and optionally 
os.path.expanduser() for tilde expansion and os.path.expandvars() for 
variable expansion, other things that are normally taken care of by the 
shell).
-- 
Michael Hoffman



More information about the Python-list mailing list