Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - bug in os.system?

 
Thread Tools Search this Thread
Old 10-18-2005, 06:33 AM   #1
Default bug in os.system?


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
  Reply With Quote
Old 10-18-2005, 07:08 AM   #2
wjzrules@gmail.com
 
Posts: n/a
Default Re: bug in os.system?

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

  Reply With Quote
Old 10-18-2005, 07:30 AM   #3
Steve Holden
 
Posts: n/a
Default Re: bug in os.system?

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/

  Reply With Quote
Old 10-18-2005, 09:53 AM   #4
Fredrik Lundh
 
Posts: n/a
Default Re: bug in os.system?

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>



  Reply With Quote
Old 10-18-2005, 09:29 PM   #5
nicksjacobson@yahoo.com
 
Posts: n/a
Default Re: bug in os.system?

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!

  Reply With Quote
Old 10-19-2005, 02:09 AM   #6
mensanator@aol.com
 
Posts: n/a
Default Re: bug in os.system?


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!


  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump