By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

bug in os.system?

P: n/a
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.

Oct 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
What happens when you try it without the single quotes?
result = os.system("pythonbugtest.exe" "test")

Oct 18 '05 #2

P: n/a
wj******@gmail.com wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 18 '05 #3

P: n/a
ni***********@yahoo.com wrote:
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>

Oct 18 '05 #4

P: n/a
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!

Oct 18 '05 #5

P: n/a

ni***********@yahoo.com wrote:
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.


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!


Oct 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Browse more Python Questions on Bytes