Ogs*_*Ogs 1 python shell subprocess
我一直在尝试如何通过将和分配给值来不打印Python的shell输出:subprocess.call()open(os.devnull, 'w')subprocess.PIPEstdout
subprocess.call(command, stdout=open(os.devnull, 'w'), shell=True)
Run Code Online (Sandbox Code Playgroud)
和
subprocess.call(command, stdout=subprocess.PIPE, shell=True)
Run Code Online (Sandbox Code Playgroud)
这两行都command谨慎地执行存储在变量中的 shell 命令,即不在终端上输出。但是,我不知道两者之间的区别。我刚开始使用subprocess.
/ogs
第一种方法是将标准输出重定向到文件(/dev/null在 POSIX 中),而第二种方法是构建一个PIPE将输出重定向到特定流的方法。
subprocess.PIPE命令 help() 引用的 官方定义: “该模块允许您生成进程,连接到它们的输入/输出/错误管道,并获取它们的返回代码。”
我想说这个方法就像:我们只是将一些东西放在消息队列(内存)中一段时间以供以后使用。但subprocess.call只返回状态码。看来您无法引用 的返回值,subprocess.call(command, stdout=open(os.devnull, 'w'), shell=True)因此无法引用 的值subprocess.call(command, stdin=the_stdout, shell=True)。在两个命令之间建立联系是很困难的。
基于本文中的信息:http://blog.acipo.com/running-shell-commands-in-python/
还有 Python 2.7 文档:https://docs.python.org/2/library/subprocess.html
建议我们可以使用Popencommunicate()
Popen是Python 3提供的高级类。有一个很好的资源: https: //stackabuse.com/pythons-os-and-subprocess-popen-commands/
| 归档时间: |
|
| 查看次数: |
7052 次 |
| 最近记录: |