Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
捕获shell执行输出的简单python命令?_Python_Shell_Capture - Fatal编程技术网

捕获shell执行输出的简单python命令?

捕获shell执行输出的简单python命令?,python,shell,capture,Python,Shell,Capture,可能重复: 当我想要捕获shell执行输出时,我会这样做 declare TAGNAME=`git describe --tags` 简单。我在Python中查找了这个,但是它们中的大多数看起来非常复杂。最简单的方法是什么?是的,我知道我可以做一个函数,但我想知道预定义函数是否存在 tagname = theFunc('git describe --tags') 尝试: 你应该看看这个模块。您还可以使用它捕获命令的输出。手册页面中有许多关于如何使用模块的示例 例如: output=sub

可能重复:

当我想要捕获shell执行输出时,我会这样做

declare TAGNAME=`git describe --tags` 
简单。我在Python中查找了这个,但是它们中的大多数看起来非常复杂。最简单的方法是什么?是的,我知道我可以做一个函数,但我想知道预定义函数是否存在

tagname = theFunc('git describe --tags')
尝试:


你应该看看这个模块。您还可以使用它捕获命令的输出。手册页面中有许多关于如何使用模块的示例

例如:

output=subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE,
    stderr=subprocess.PIPE).communicate()

结果是一个从命令中捕获的带有stdout和stderr的元组。

需要为返回的字符串调用
.strip('\n')
。@Eonil Why调用
.strip('\n')
?有一个
\n
真的重要吗?@xiaomao是的。因为我将把字符串传递给另一个shell命令。真是太好了,你可以得到意想不到的结果。
output=subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE,
    stderr=subprocess.PIPE).communicate()