使用python运行.bat/.exe并向.bat/.exe输入命令行

Python脚本尝试通过Cygwin.bat执行安卓编译,遇到命令执行问题,寻求解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import subprocess
import os
import sys

bat_file = r'C:\softwore\cyg_win\Cygwin.bat'
#command = 'cd D:/测试/源代码/test && $NDK/ndk-build'
# 获取当前运行路径
current_directory = os.path.dirname(sys.executable)
current_directory = current_directory.replace('\\','/')
command = 'cd ' + current_directory +  ' && $NDK/ndk-build'

# 执行批处理文件 在这里已经向批处理传递命令行了 但是不知道为什么不执行
process = subprocess.Popen([bat_file, '/C', command], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

# 向控制台发送命令行输入  所以在这重新向批处理发送命令行
input_command = command  # 替换为你需要输入的命令行
process.stdin.write(input_command.encode())
process.stdin.flush()

# 获取命令行输出
output, error = process.communicate()

# 如果有错误则打印错误信息和输出
if error:

    # 打印输出和错误信息
    print(output.decode())

    print(error.decode())
    os.system("pause")

要执行一个批处理进行安卓编译,每次都要复制路径、手敲执行的命令行,所以写了脚本

PS:目前还不清楚为什么在打开批处理时向批处理传递命令行不执行,后续查到结果再补充,有大神路过也请告知一下

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值