【问题标题】:Python. How to execute a system command(Windows) avioding console? [duplicate]Python。如何在控制台中执行系统命令(Windows)? [复制]
【发布时间】:2013-06-19 23:07:33
【问题描述】:

现在我正在使用 pythonw.exe 运行我的 scrypt,当执行以下命令时出现 cmd:

r = subprocess.call('net stop tomcat7', shell=False)
print r

代码执行时如何让cmd不出现?

【问题讨论】:

  • 试试shell = True

标签: python windows console


【解决方案1】:

尝试将shell参数设置为'True'。

r = subprocess.call('net stop tomcat7', shell=True)  
print r

【讨论】:

    【解决方案2】:

    我将此 startupinfo 与 subprocess.Popen 一起使用(在 this project 中):

    subprocess.STARTF_USESHOWWINDOW = 1
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    
    subprocess.Popen(
        [app] + args,
        startupinfo=startupinfo,
        stderr=subprocess.PIPE,
        stdout=subprocess.PIPE)
    

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      相关资源
      最近更新 更多