您的位置:首页 > 编程语言 > Python开发

python调用ahk,并读取ahk的执行结果。

2016-04-21 10:53 585 查看
目前使用ahk脚本(已编译成独立exe)来执行一个动作,而python主程序负责调用这些不同的ahk程序。

以前是通过系统粘贴板来交互数据,即ahk程序启动后清除粘贴板,在结束时将执行情况已文字方式拷贝到粘贴板,python程序在等待ahk进程结束后再从粘贴板中读取执行结果。很显然这种方式有些弊端,比如不能同时执行多个ahk程序,还要避免人在电脑上进行ctrl-c ctrl-v的操作等。

研究了python的subprocess库,发现可以利用stdin stdout等来在进程间传递数据:

subprocess.check_output(args, *, input=None, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None)
Run command with arguments and return its output.
By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.
<p>    To also capture standard error in the result, use <tt class="docutils literal"><span class="pre">stderr=subprocess.STDOUT</span></tt>:</p>
在AHK脚本中可以这样写回数据:

stdout := FileOpen("*", "w")
stdout.write(result)
stdout.close()


一个实际的例子:

ahk_res = subprocess.check_output([r"my_ahk.exe", "param1", "param2"])
print(ahk_res.decode("gbk"))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: