简体   繁体   English

Python:在没有cmd界面或隐藏或在后台的子Shell中执行命令

[英]Python: Execute a command in a subshell without cmd interface or hidden or in background

I would like to know how I could execute a command whitout appears the cmd window. 我想知道如何执行命令,出现cmd窗口。 My code is in Python and the OS is Windows7. 我的代码是使用Python,操作系统是Windows7。

The problematic line is: os.system(pathandarguments) 有问题的行是: os.system(pathandarguments)

The program works fine, execute the given path with the arguments but I loose the control of my program because my program window minimizes, I see cmd window a seconds and then my window program dont maximizes. 程序运行良好,使用参数执行给定的路径,但是由于程序窗口最小化,我松开了程序的控制,我看到cmd窗口一秒钟,然后窗口程序没有最大化。

I want to execute the string pathandarguments without minimize my principal window. 我想执行字符串pathandarguments而不最小化我的主体窗口。 I prefer, if its possible, dont show cmd window. 我希望,如果可能的话,不要显示cmd窗口。 I tried differents ways to make this: 我尝试了不同的方法来做到这一点:

os.system(pathandarguments) = works fine but minimizes my program window. os.system(pathandarguments) =可以正常工作,但是可以最小化我的程序窗口。

os.popen(pathandarguments) = ERROR: CThread::staticThread : Access violation at 0x77498c19: Writing location 0x00000014 (Don't work) os.popen(pathandarguments) = ERROR: CThread::staticThread : Access violation at 0x77498c19: Writing location 0x00000014 (不起作用)

subprocess.Popen([pathandarguments], shell=False) = Exception in python script's onAction (Don't work) subprocess.Popen([pathandarguments], shell=False) = python脚本的onAction中的异常(不起作用)

Thanks in advance. 提前致谢。

EDIT @martineau, The problem is not that I cannot import process, revising the log of my app I saw the problem is with import process at line 146: EDIT @martineau,问题不在于我无法导入过程,修改了我的应用程序的日志,我发现问题出在146行的导入过程中:

13:42:20 T:4116 M:2156859392  NOTICE: import win32api
13:42:20 T:4116 M:2156859392  NOTICE: ImportError
13:42:20 T:4116 M:2156859392  NOTICE: :
13:42:20 T:4116 M:2156859392  NOTICE: No module named win32api

I dont have module win32api. 我没有模块win32api。

For a long time I've been using an open source Python module for process control called process-python . 很长时间以来,我一直在使用一个名为process-python的开源Python模块进行过程控制。 The Project Status there says "In its current state it was used heavily in the commercial Komodo IDE project." 那里的项目状态显示“在当前状态下,它已在商业Komodo IDE项目中大量使用。” It's multiplatform, but one of the main reasons I started using it was because on Windows it will spawn a process without a console window. 它是多平台的,但是我开始使用它的主要原因之一是因为在Windows上它将生成没有控制台窗口的进程。 It's very simple to use. 使用非常简单。 Here's a trivial example: 这是一个简单的例子:

import process

p = process.ProcessOpen([eventfilepath]) # open text file with associated program
ignored_exitstatus = p.wait()

Hope this helps. 希望这可以帮助。

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

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