Python子进程模块使用

标签 python subprocess

如何指定python模块子进程使用哪个目录来执行命令?我想运行一个 C 程序并在 Python 中近乎实时地处理它的输出。

我想执行命令(这会运行我的 C 程序的 makefile:

make run_pci

在:

/home/ecorbett/hello_world_pthread

我还想在Python中处理C程序的输出。我是 Python 新手,所以示例代码将不胜感激!

谢谢, 爱德华

最佳答案

cwd 参数用于 Popencallcheck_callcheck_output 。例如

subprocess.check_output(["make", "run_pci"],
                        cwd="/home/ecorbett/hello_world_pthread")

编辑:好的,现在我明白你所说的“近实时”是什么意思了。尝试一下

p = subprocess.Popen(["make", "run_pci"],
                     stdout=subprocess.PIPE,
                     cwd="/home/ecorbett/hello_world_pthread")
for ln in p.stdout:
    # do processing

关于Python子进程模块使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11419643/

相关文章:

python - 如何简化不同键的建议字典

python - 全局范围内的描述符?

pdf - python 3.6 : FileNotFoundError: [WinError 2] The system cannot find the file specified when running PDF-DIFF tool

Python脚本从终端执行,但在使用子进程时不执行?

python - 什么是坏魔数(Magic Number)错误?

python - 为什么这个tensorflow.keras代码需要大量内存?

python - 检查子进程调用是否成功执行

python - 使用子进程 popen 运行列表命令字符串并获取输出

python - 将 Nmap 子进程中的数据直接存储到列表中

python - 如何通过解析导入来组合并获取单个 Python 文件