Python:os.system 执行 meld 命令在 Windows 中不起作用



我正在用Python编写一个出色的插件。但我对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窗口只闪烁了一秒钟(似乎在执行什么),然后就消失了。我将执行的字符串打印出来,并将字符串复制到cmd窗口中执行。它在管理模式和非管理模式下都能很好地执行。我试着在执行之前添加一个暂停:

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

点击回车键后按任意键继续,效果也很好。。。线

不知道如何调试它,以及它失败的原因是什么。

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)

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

继续提问如何在windows上使用提升的权限运行python脚本,以获取有关该主题的更多信息。

最新更新