简体   繁体   English

为什么Windows XP上Python的os.system()命令在开始运行时需要两个双引号?

[英]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. 我在Windows XP上遇到了一些非常奇怪的行为。 I'm using Python to execute a command to open a browser using a shortcut file in a folder on the desktop. 我正在使用Python执行命令,以使用桌面上文件夹中的快捷方式文件打开浏览器。

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. 我可以通过在该命令的开头放置echo来确认这是正确的。 (ie os.system('echo "C:\\Documents and Settings\\blah\\blah chrome.google.com/webstore"') ) (即os.system('echo“ C:\\ Documents and Settings \\ blah \\ blah chrome.google.com/webstore”'))

Using echo returns the following: 使用echo返回以下内容:

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

That looks like a fine Windows command, yes? 看起来像Windows命令一样,是吗? 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. 'C:\\ Documents'不被识别为内部或外部命令。

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. 对我来说,它看起来像是空字符串,带有未转义空格的未引用路径,然后是带空格开头的带引号的url。

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. 将该回声结果粘贴到命令行失败,并且之前出现“ C:\\ Documents not defined”错误,但是Python命令无论如何都会将浏览器打开到正确的页面。

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. PS此行为在XP之后的每个Windows操作系统上都完全不同。 For Vista and newer the command is: 对于Vista和更高版本,命令为:

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? C:\\Documents and Settings\\..看到2个空格吗? otherwise it will pick up C:\\Documents as a binary and and as the first param, Settings\\.. as the other param.. and so on. 否则它将选择C:\\Documents作为二进制文件, and作为第一个参数,将Settings\\..作为另一个参数。 This way youre saying: this whole thing is a binary C:\\Documents and Settings\\.. and chrome.google.com/webstore is my argument. 您这样说:整个事情是一个二进制C:\\Documents and Settings\\..chrome.google.com/webstore是我的观点。

Make sense? 说得通?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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