Python:os.system执行meld命令不能在Windows中运行

时间:2015-12-18 05:24:40

标签: python windows sublimetext3

我正在用Python编写一个sublime插件。但我仍然是Python语言的新手。我正在使用这一行在Python中调用meld:

if meld_cmd == None:
   meld_cmd = "C:\\Program Files (x86)\\Meld\\meld\\meld"
os.system('"%s" "%s" "%s" ' %(meld_cmd, source_name, dest_name))

它在Windows中无法正常运行。 cmd窗口闪烁一秒钟(似乎执行某些操作)然后消失。我在sublime中打印执行的字符串并将字符串复制到cmd窗口并执行。它在管理员和非管理员模式下都能很好地执行。我尝试在执行前添加暂停:

os.system('pause && "%s" "%s" "%s" ' %(meld_cmd, source_name, dest_name))

按下任意键继续...行后点击输入也很有效。

不确定如何调试它以及失败的原因是什么。

2 个答案:

答案 0 :(得分:1)

当命令以引号开头并且引号超过两个时,

cmd.exe /c command有一个解析怪癖。准确的规则在cmd /?提供的在线帮助文​​本中进行了解释:

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 (") characters:

    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 character.

要解决这个问题,只需将整个命令包装在引号中,例如: '""%s" "%s" "%s""' % (meld_cmd, source_name, dest_name)

答案 1 :(得分:0)

问题可能是您需要使用管理员权限执行该脚本。

继续问How to run python script with elevated privilege on windows以获取有关该主题的更多信息。