python - Python子进程在交互模式下工作但不在脚本中
问题描述
我正在尝试从 python 执行 shell 脚本并捕获输出。
我正在使用以下几行:
x = subprocess.run(["sh", "/path/to/script/myscript.bash"], stdout=subprocess.PIPE)
print(x.stdout.decode('utf8'))
这在 python 交互模式下运行时工作正常,但在 python 脚本中执行时它只是挂起,我必须使用 ctrl+Z 来终止 python 脚本。我错过了什么?
在过去使用 python 2.7 时,我刚刚使用了如下一行:
x = subprocess.Popen(args='sh /path/to/script/myscript.bash', stdout=subprocess.PIPE, shell=True)
scriptOutput = str(x.communicate()[0])
这似乎不适用于我正在运行的 python 3.6。
解决方案
推荐阅读
- python - 如何在 ansible.cfg 文件中添加多个库路径
- sql - Bigquery - json_extract 数组中的所有元素
- javascript - 尝试在页面刷新后保持导航栏中选定的 div 处于活动状态
- jquery - 带有变量的正则表达式构造函数不起作用,但似乎正确
- python-3.x - 如何让 Tensorflow Served 模型从传入的输入而不是本地批处理文件中提取?
- java - java.sql.SQLTransactionRollbackException:在请求的时间内无法获得锁
- c++ - 管理 Lua 轻量级用户数据的正确方法
- c - 是否可以抑制 Xcode (clang) 分析器报告的问题实例?
- python - 如何使用迭代函数为同一维度的numpy数组分配新的不同值
- c# - Unity3D - Google Play 游戏登录总是失败