This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Quote handling in os.system & os.popen
Type: Stage:
Components: Windows Versions: Python 2.2
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: jretz, tim.peters
Priority: normal Keywords:

Created on 2002-02-03 17:36 by jretz, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Messages (2)
msg9108 - (view) Author: Jimmy Retzlaff (jretz) Date: 2002-02-03 17:36
On Python 2.2 under Windows XP:

    os.system('"notepad" ""')

does not work as expected. It appears that os.system 
attempts to run:

    notepad" "

A workaround is to use:

    os.system('""notepad" """')

Both of the following work as expected:

    os.system('notepad ""')

os.popen exhibits the same behaviour. In naive 
testing, the following hack seems to make things 

    os_system = os.system
    os.system = lambda command: os_system('"%s"' % 

This may suggest a potential fix in the C code - or 
it may simply offend the sensibilities of those more 
knowledgeable than me. :)
msg9109 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2002-03-10 01:47
Logged In: YES 

Sorry, I'm closing as "Won't Fix".  os.system() and os.popen
() are barely usable on Windows, and it's going to remain 
that way until Python grows its own command shell.  Before 
that, it's at the mercy of what the MS shells happen to 
do.  In the case of XP's cmd.exe, you're a victim of 
documented (by MS) behavior:  see the /C and /K options to 

If /C or /K is specified, then the remainder of the command 
line after the switch is processed as a command line, where 
the following logic is used to process quote (") 

    1. If all of the following conditions are met, then 
quote characters on the command line are preserved: 

        - no /S switch 
        - exactly two quote characters 
        - no special characters between the two quote 
characters, where special is one of: &<>()@^| 
        - there are one or more whitespace characters 
between the two quote characters 
        - the string between the two quote characters is 
the name of an executable file. 

    2. Otherwise, old behavior is to see if the first 
character is a quote character and if so, strip the leading 
character and remove the last quote character on the 
command line, preserving any text after the last quote 

You're a victim of clause #2 there.  The MS shells aren't 
consistent about these rules, so there's nothing Python can 
do to try to out-guess them, short of heroic efforts.  For 
example, if we took your suggestion, things that work fine 
today under Win98's would suddenly break (W98 
does *not* strip the new quotes you're adding, so "Bad 
command or file name" is the usual result).
Date User Action Args
2022-04-10 16:04:56adminsetgithub: 36021
2002-02-03 17:36:16jretzcreate