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