Linux 如何将执行的shell命令的结果存储在python中的变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8659275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store the result of an executed shell command in a variable in python?
提问by user567879
I need to store the result of a shell command that I executed in a variable, but I couldn't get it working. I tried like:
我需要将我在变量中执行的 shell 命令的结果存储起来,但我无法让它工作。我试过:
import os
call = os.system("cat syscall_list.txt | grep f89e7000 | awk '{print }'")
print call
But it prints the result in terminal and prints the value of call as zero, possibly indicating as success. How to get the result stored in a variable?
但它会在终端中打印结果并将 call 的值打印为零,这可能表示成功。如何将结果存储在变量中?
采纳答案by Rob Wouters
Use the subprocess
module instead:
改用subprocess
模块:
import subprocess
output = subprocess.check_output("cat syscall_list.txt | grep f89e7000 | awk '{print }'", shell=True)
Edit: this is new in Python 2.7. In earlier versions this should work (with the command rewritten as shown below):
编辑:这是 Python 2.7 中的新功能。在早期版本中,这应该可以工作(使用如下所示重写的命令):
import subprocess
output = subprocess.Popen(['awk', '/f89e7000/ {print }', 'syscall_list.txt'], stdout=subprocess.PIPE).communicate()[0]
As a side note, you can rewrite
作为旁注,您可以重写
cat syscall_list.txt | grep f89e7000
To
到
grep f89e7000 syscall_list.txt
And you can even replace the entire statement with a single awk
script:
你甚至可以用一个awk
脚本替换整个语句:
awk '/f89e7000/ {print }' syscall_list.txt
Leading to:
导致:
import subprocess
output = subprocess.check_output(['awk', '/f89e7000/ {print }', 'syscall_list.txt'])
回答by enderskill
commands.getstatusoutputwould work well for this situation. (Deprecated since Python 2.6)
commands.getstatusoutput在这种情况下可以很好地工作。(自 Python 2.6 起已弃用)
import commands
print(commands.getstatusoutput("cat syscall_list.txt | grep f89e7000 | awk '{print }'"))
回答by AMADANON Inc.
All the other answers here are fine answers. In many situations, you do need to run external commands.
这里的所有其他答案都是很好的答案。在许多情况下,您确实需要运行外部命令。
This specific example has another option: you can read the file, process it line by line, and do something with the output.
这个特定的例子还有另一个选择:你可以读取文件,一行一行地处理它,并对输出做一些事情。
While this answer doesn't work for the "more general question being asked", I think it should always be considered. It is not always the "right answer", even where possible. Remembering this (easier), and knowing when (not) to apply it (more difficult), will make you a better programmer.
虽然这个答案不适用于“提出的更普遍的问题”,但我认为应该始终考虑它。即使在可能的情况下,它也不总是“正确答案”。记住这一点(更容易),并知道何时(不)应用它(更困难),将使您成为更好的程序员。
回答by Prakash D
In python 3 you can use
在python 3中你可以使用
import subprocess as sp
output = sp.getoutput('whoami --version')
print (output)
``
回答by Ravishankar Sivasubramaniam
os.popen works for this. popen - opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read. split('\n') converts the output to list
os.popen 为此工作。popen - 打开一个管道到或从命令。返回值是一个连接到管道的打开文件对象,可以读取。split('\n') 将输出转换为列表
import os
list_of_ls = os.popen("ls").read().split('\n')
print list_of_ls
import os
list_of_call = os.popen("cat syscall_list.txt | grep f89e7000 | awk '{print }'").read().split('\n')
print list_of_call