简体   繁体   English

我如何将 shell 命令 output 存储到 python 中的变量?

[英]How would I store the shell command output to a variable in python?

Code:代码:

# cat mylinux.py
# This program is to interact with Linux

import os

v = os.system("cat /etc/redhat-release")

Output: Output:

# python mylinux.py
Red Hat Enterprise Linux Server release 7.6 (Maipo)

In the above output, the command output is displayed regardless of the variable I defined to store the output.在上面的output中,不管我定义的用来存储output的变量,都会显示命令output。

How to store the shell command output to a variable using os.system method only?如何仅使用 os.system 方法将 shell 命令 output 存储到变量中?

By using module subprocess .通过使用模块subprocess It is included in Python's standard library and aims to be the substitute of os.system .它包含在 Python 的标准库中,旨在成为os.system的替代品。 (Note that the parameter capture_output of subprocess.run was introduced in Python 3.7) (注意Python 3.7引入了subprocess.run的参数capture_output

>>> import subprocess
>>> subprocess.run(['cat', '/etc/hostname'], capture_output=True)
CompletedProcess(args=['cat', '/etc/hostname'], returncode=0, stdout='example.com\n', stderr=b'')
>>> subprocess.run(['cat', '/etc/hostname'], capture_output=True).stdout.decode()
'example.com\n'

In your case, just:在你的情况下,只是:

import subprocess

v = subprocess.run(['cat', '/etc/redhat-release'], capture_output=True).stdout.decode()

Update: you can split the shell command easily with shlex.split provided by the standard library.更新:您可以使用标准库提供的shlex.split轻松拆分 shell 命令。

>>> import shlex
>>> shlex.split('cat /etc/redhat-release')
['cat', '/etc/redhat-release']
>>> subprocess.run(shlex.split('cat /etc/hostname'), capture_output=True).stdout.decode()
'example.com\n'

Update 2: os.popen mentioned by @Matthias更新 2: os.popen提到的 os.popen

However, is is impossible for this function to separate stdout and stderr.但是,这个 function 不可能将 stdout 和 stderr 分开。

import os

v = os.popen('cat /etc/redhat-release').read()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何使用python将exec_command的输出存储在paramiko中 - How would you store the output of exec_command in paramiko with python Python:在变量中存储命令 output - Python: Store command output in variable 如何在python中的变量中存储执行的shell命令的结果? - How to store the result of an executed shell command in a variable in python? 如何检索上一个命令的输出并将其保存在Python交互式shell中的变量中? - How do I retrieve the output of a previous command and save it in a variable inside the Python interactive shell? 如何从 Python 中的 shell 命令捕获输出? - How can I capture the output from a shell command in Python? 如何在命令行上调用python脚本,就像调用普通的shell命令(如cp)一样? - How do I call a python script on the command line like I would call a common shell command, such as cp? 如何将'count'变量的输出存储到列表中? (蟒蛇) - How do i store the output of the 'count' variable into a list? (python) 使用python执行shell命令,将output重定向到一个变量 - Use python to execute shell command and redirect the output to a variable 如何在 python 中为 shell 命令使用变量值? - How to use variable value for shell command in python? 如何将 bash 命令的输出放置到 Python 变量中? - How do I place output of bash command to Python variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM