Running into problems with os.popen

Donn Cave donn at u.washington.edu
Fri Mar 21 12:37:17 EST 2003


Quoth John Hazen <python-list at hazen.net>:
| On Thu, Mar 20, 2003 at 07:02:06AM -0600, Stephen Boulet wrote:
|> The command is:
|> 
|> pth = os.path.join(curDir,self.fileName)
|> command = 'hdp dumpsds -c "%s"' % pth
|
| If the filename (or any of the directories on the path) has a
| space in it, the shell (depending on your OS) may split what you
| *mean* as one argument into two.
|
| If that's the kind of truncation you're talking about, try this:
|
| command = "hdp dumpsds -c '%s'" % pth
|
| Even though they're the same to python, to the shell single quotes will
| contain the space, while double quotes will not.

Well, actually in that respect they're the same to the shell, too.
The difference with the (Bourne) shell is that variable substitution
occurs in the double quotes and not in the single quotes, which is
relatively unlikely to be a problem with normal paths - '$' is not
common in file names.  Of course the other difference (or similarity)
is that either kind of quote is sensitive to occurrence of itself
in the data.

Anyway, this is just the kind of thing that is so much better done
with a parameter list, with popen2 or spawnv, rather than a shell
command line, e.g., popen2.popen2(['hdp', 'dumpsds', '-c', pth])

Thus avoiding such problems altogether.

	Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list