如何在 Python 中创建子进程? [英] How do I create a subprocess in Python?

查看:42
本文介绍了如何在 Python 中创建子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个流程的子流程.

展示如何完成此操作的工作示例是什么?

解决方案

子流程开始

a> 文档.

如果你想得到输出:

<预><代码>>>>导入子流程>>>output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0]>>>输出'Linux'

如果你只是想调用而不处理输出:

<预><代码>>>>subprocess.call(['echo', 'Hi'])你好0

subprocess.check_call 是一样的,除了它抛出一个CalledProcessError 以防使用无效参数调用命令.

很好的子流程教程.

I would like to create a subprocess of a process.

What would be a working example which shows how to accomplish this?

解决方案

Start with the subprocess documentation.

If you want to get the output:

>>> import subprocess
>>> output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0]
>>> output
'Linux'

If you just want to call and not deal with the output:

>>> subprocess.call(['echo', 'Hi'])
Hi
0

subprocess.check_call is the same except that it throws up a CalledProcessError in case the command is called with invalid parameters.

A good subprocess tutorial.

这篇关于如何在 Python 中创建子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆