bug in os.system?

N

nicksjacobson

The following code fails (pythonbugtest.exe takes one parameter, a
string):

import os
result = os.system('"pythonbugtest.exe" "test"')
assert(result == 0)

The error message is:

'pythonbugtest.exe" "test' is not recognized as an internal or external
command, operable program or batch file.
Traceback (most recent call last):
File "C:\Nick\!My Programs\Python\bugtest\python1.py", line 8, in ?
assert(result == 0)
AssertionError


If I remove the quote marks around "pythonbugtest.exe" or "test", it
works fine. But sometimes I need those quote marks, if e.g. there are
spaces in filenames.

I think this is a bug?

I'm running Python 2.4.1 on Windows XP Pro.
 
W

wjzrules

What happens when you try it without the single quotes?
result = os.system("pythonbugtest.exe" "test")
 
S

Steve Holden

What happens when you try it without the single quotes?
result = os.system("pythonbugtest.exe" "test")
That would be equivalent to

result = os.system("pythonbugtest.exetest")

which almost certainly won't do anything useful.

regards
Steve
 
F

Fredrik Lundh

The following code fails (pythonbugtest.exe takes one parameter, a
string):

import os
result = os.system('"pythonbugtest.exe" "test"')
assert(result == 0)

The error message is:

'pythonbugtest.exe" "test' is not recognized as an internal or external
command, operable program or batch file.
Traceback (most recent call last):
File "C:\Nick\!My Programs\Python\bugtest\python1.py", line 8, in ?
assert(result == 0)
AssertionError

If I remove the quote marks around "pythonbugtest.exe" or "test", it
works fine. But sometimes I need those quote marks, if e.g. there are
spaces in filenames.

I think this is a bug?

yup, but unfortunately, it's a bug at the windows level, not in Python. from what
I can tell, the problem is that cmd.exe cannot parse the command string it's given
by the C-level system() call.

possible workarounds:

1. get rid of the quotes around the command name:

result = os.system('pythonbugtest.exe "test"')

2. add an extra quote (!) before the quoted command name:

result = os.system('""pythonbugtest.exe" "test"')

3. use os.spawn or the subprocess module instead.

</F>
 
N

nicksjacobson

OK, I give up. Why does workaround #2 work?

Also, I didn't realize this before, but when you call os.spawnv, the
argument list you pass starts with the name of the executable you're
calling! When you call a program from cmd.exe, that program name is
the first parameter automatically. But with spawnv, you do that
manually!

Anyway, thanks for your help!
 
M

mensanator

OK, I give up. Why does workaround #2 work?

Well, there was a time when the cmd prompt treated all
spaces as delimiters, so
>cd My Documents

would fail. Nowadays you can do that successfully and even
>cd My Documents\My Pictures

works.

In the old days, if a directory had a space, you had to
enclose it in quotes
>cd "My Documents"

But you didn't actually need to include the trailing quote,
so you could get away with
>cd "My Documents

I'm sure if you looked it up, Microsoft would say

This behaviour is by design.
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top