如何在Python脚本中调用外部命令(就像我在Unix shell或Windows命令提示符下键入它一样)?
我正在尝试从python启动一个完全独立的进程.我不能使用像os.startfile这样简单的东西,因为我需要传递参数.目前我正在使用subprocess.popen,它让我90%的方式.
args = ["some_exe.exe", "some_arg", "another_arg"]
subprocess.Popen(args, creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
使用带有分离创建标志和std*管道的popen会启动一个新进程,该进程在父进程终止后继续存在.这一切都很好.问题是新的'子'进程仍然保留了父进程的幻象句柄.因此,如果我尝试卸载父exe(我的python脚本通过pyinstaller捆绑到exe),msiexec抱怨父exe仍在使用中.
因此,目标是生成一个完全独立的进程来运行"some_exe.exe",它没有任何句柄回原始进程.
注意:这适用于Windows XP及更高版本.我正在开发Win7.
我的代码简单如下:
file = 'C:\\Exe\\First Version\\filename.exe'
os.system(file)
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序时,会引发windowserror,找不到指定的文件.我发现问题与"第一版"之间的空白有关.那么我能找到一种绕过这个问题的方法吗?
PS:如果变量'file'将作为arg传递到另一个func中怎么办?
我正在尝试从Python运行一个外部的独立程序.这通常不是问题,但程序是游戏,并且内置了Python解释器.当我使用subprocess.Popen时,它启动单独的程序,但是在原始程序的Python实例下这样做,以便它们共享第一个Python控制台.我可以很好地结束第一个程序,但我宁愿有单独的控制台(主要是因为我有隐藏的控制台启动,但是当我使用subprocess.POpen从Python启动程序时会显示它).
如果我可以完全自己开始第二个程序,我想它就好像我只是"双击它".此外,os.system将无法工作,因为我的目标是跨平台兼容性,而且只能在Windows上使用.
我是Python编程的新手.我想从Python程序执行一个shell命令"at".任何一位Python专家都可以帮助我吗?提前致谢.
重复编辑:不,我这样做但它不想启动Firefox.我正在做一个cortana/siri助手的事情,我想让它说我在说些什么时打开一个网页浏览器.所以我已经完成了if部分,但我只是需要它来启动firefox.exe我尝试了不同的东西,我得到一个错误.这是代码.请帮忙!它适用于打开记事本,但不适用于Firefox.
#subprocess.Popen(['C:\Program Files\Mozilla Firefox\firefox.exe']) opens the app and continues the script
#subprocess.call(['C:\Program Files\Mozilla Firefox\firefox.exe']) this opens it but doesnt continue the script
import os
import subprocess
print "Hello, I am Danbot.. If you are new ask for help!" #intro
prompt = ">" #sets the bit that indicates to input to >
input = raw_input (prompt) #sets whatever you say to the input so bot can proces
raw_input (prompt) #makes an input
if input == "help": #if the input is that …Run Code Online (Sandbox Code Playgroud) 这与这个问题有关,但有不同的看法.
在Ubuntu中,我使用Autokey,它使用python自动化它观察到的击键.所以我已经<super>+e映射到打开<shift>+<super>+3Gedit,打开OOwriter等等.当我进行其中一个调用时,我不能再创建另一个调用,直到上一个调用的程序退出.
以下是它执行的脚本示例:
import subprocess
subprocess.call("/opt/openoffice.org3/program/scalc")
Run Code Online (Sandbox Code Playgroud)
...使用相同的行为:
import os
os.system("/opt/openoffice.org3/program/scalc")
Run Code Online (Sandbox Code Playgroud)
这一切都在我之前的Ubuntu 10.04LTS中顺利运行,但事情发生了变化,我不能再重复这些调用了.
你能不能帮我解决如何分叉或做一些事情从subprocess.call()"回来"而不等待程序退出?我试过nohup和后台,/opt/openoffice.org3/program/scalc &但那些什么都不做(可能在Autokey和Py中打破了一些东西)
答:下面的答案实际上并没有起作用,但让我窥探更多,我发现另一个SO答案对我的情况有效!
#Enter script code -- mapped to <super>+e
import thread
thread.start_new_thread(os.system,('gedit',))
Run Code Online (Sandbox Code Playgroud)
这完全奏效!! 我可以连续打<super>+e2到3次,并且不断向gedit添加标签.:)此脚本使Autokey的行为就像在命令行中键入引号中的命令一样.
我一直在寻找原始问题的答案..如何(以编程方式)确定我的win32api.ShellExecute语句成功执行,如果成功执行,则执行os.remove()语句.
研究我发现ShellExecute()调用返回HINSTANCE.进一步挖掘我发现如果成功,ShellExecute()将返回HINSTANCE> 32.我的问题/问题是,如何使用它来控制程序的其余部分?我尝试使用if HINSTANCE> 32:语句来控制下一部分,但我得到了一条NameError: name 'hinstance' is not defined消息.通常这不会让我感到困惑,因为这意味着我需要在引用之前定义变量'hinstance'; 但是,因为我认为ShellExecute应该返回HINSTANCE,我认为它可以使用吗?
这是我的完整代码,我试图实现这一点.请注意,在我的print_file()中,我将hinstance分配给完整的win32api.ShellExecute()命令,以尝试捕获hinstance并在函数末尾显式返回它.这也不起作用.
import win32print
import win32api
from os.path import isfile, join
import glob
import os
import time
source_path = "c:\\temp\\source\\"
def main():
printer_name = win32print.GetDefaultPrinter()
while True:
file_queue = [f for f in glob.glob("%s\\*.txt" % source_path) if isfile(f)]
if len(file_queue) > 0:
for i in file_queue:
print_file(i, printer_name)
if hinstance > 32:
time.sleep(.25)
delete_file(i)
print "Filename: %r has printed" % i
print
time.sleep(.25)
print
else:
print …Run Code Online (Sandbox Code Playgroud) 我正在编写一个基本上是截图工具的Python 程序。我希望能够运行我的程序,使用鼠标单击并拖动选择屏幕截图区域,然后让程序保存该图像。
我正在尝试使用此处找到的代码:http ://pyscreenshot.readthedocs.io/en/latest/
#-- include('examples/showgrabfullscreen.py') --#
import pyscreenshot as ImageGrab
if __name__ == '__main__':
# grab fullscreen
im = ImageGrab.grab()
# save image file
im.save('screenshot.png')
# show image in a window
im.show()
#-#
Run Code Online (Sandbox Code Playgroud)
(在“抓取并显示屏幕的一部分”下),但这不允许用户单击并拖动。有谁知道我该怎么做?我在网上找到了一些示例,但它们都有数百行长,我认为这个简单的程序不应该那么长(但我可能是错的)。
谢谢!
python ×10
shell ×2
subprocess ×2
autokey ×1
bots ×1
command ×1
launch ×1
popen ×1
python-2.7 ×1
pywin32 ×1
screenshot ×1
selection ×1
shellexecute ×1
terminal ×1
ubuntu-12.04 ×1
winapi ×1
windowserror ×1