Spawn/Exec with asterisk in argument

J

jeremyfee

The spawn* and exec* functions appear to escape asterisks, I'm
guessing all shell characters too, before the spawn/exec'ed process
sees them. Is there a way around this?

Not sure if this is a bug or a "feature".

user$ touch test.txt
user$ ls -l *
-rw-r--r-- 1 user user 0 Apr 18 18:30 test.txt
user$ ls -l \*
ls: *: No such file or directory
user$ python
Python 2.3.5 (#1, Aug 12 2006, 00:08:11)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.-rw-r--r-- 1 user user 0 Apr 18 18:30 test.txt
0ls: *: No such file or directory
1
 
M

Michael Hoffman

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).
 
J

jeremyfee

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)

Thanks for the reply Michael. I used ls as a simple example, but I'm
using this with scp to transfer files and need shell expansion. It
makes sense since the shell isn't spawning the process, but it's been
a while since I've worked with ipc. I had converted to os.system
before posting, but subprocess gives me more control than I originally
hoped for

Thanks again
 
M

Michael Hoffman

Thanks for the reply Michael.

No problem.
I used ls as a simple example, but I'm
using this with scp to transfer files and need shell expansion. It
makes sense since the shell isn't spawning the process, but it's been
a while since I've worked with ipc.

I usually don't think of starting new processes as being IPC. I've only
ever seen it used to refer to communicating between processes after they
have started.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top