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

1 篇文章 0 订阅


     目前使用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"))



   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值