![]() |
|
|
|
#1 |
|
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. nicksjacobson@yahoo.com |
|
|
|
|
#2 |
|
Posts: n/a
|
What happens when you try it without the single quotes?
result = os.system("pythonbugtest.exe" "test") |
|
|
|
#3 |
|
Posts: n/a
|
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/ |
|
|
|
#4 |
|
Posts: n/a
|
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> |
|
|
|
#5 |
|
Posts: 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! |
|
|
|
#6 |
|
Posts: n/a
|
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! |
|