简体   繁体   中英

Why does Python's os.system() command on Windows XP require two double quotes at the beginning to run?

I've encountered some very strange behavior on Windows XP. I'm using Python to execute a command to open a browser using a shortcut file in a folder on the desktop.

The following line is what I expect to do the job:

    os.system(r'"C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" "chrome.google.com/webstore"')

It's a raw string literal so all the backslashes are actual backslashes. I can tell that is true by putting echo at the start of that command. (ie os.system('echo "C:\\Documents and Settings\\blah\\blah chrome.google.com/webstore"') )

Using echo returns the following:

"C:\\Documents and Settings\\you\\Desktop\\Chrome Browsers\\Google Chrome 46.lnk" "chrome.google.com/webstore"

That looks like a fine Windows command, yes? Well it is. Copying and pasting that into a command prompt runs fine. But the actual command (without echo) fails. The error states that

'C:\\Documents' is not recognized as an internal or external command.

Which is a pretty standard error for an unquoted path. But wait, the command we echoed was good, so it should run, right? I guess not...

Through trial and error I was able to find something that worked. The following line is the only way I've been able to get the browser to launch:

os.system('""C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" chrome.google.com/webstore"')

That's right, apparently the solution is to add an extra double quote at the beginning of the command and take out the double quote before the second argument.

To me that looks like empty string, unquoted path with unescaped spaces, then a quoted url that starts with a space.

If I echo that command it returns exactly what you would expect:

""C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" chrome.google.com/webstore"

But it works! Pasting that echo result into the command line fails with the "C:\\Documents not recognized" error from before, but the Python command opens the browser to the correct page anyway.

Could someone please explain what is happening here? I am really confused by this behavior because it is not at all what I expect.

PS This behavior is entirely different on every Windows OS past XP. For Vista and newer the command is:

os.system(r'"C:\Users\you\Desktop\Chrome Browsers\Google Chrome\Google Chrome 46.lnk" "chrome.google.com/webstore"')

Because there are " " spaces in your paths. C:\\Documents and Settings\\.. see the 2 spaces? otherwise it will pick up C:\\Documents as a binary and and as the first param, Settings\\.. as the other param.. and so on. This way youre saying: this whole thing is a binary C:\\Documents and Settings\\.. and chrome.google.com/webstore is my argument.

Make sense?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM