我正在为一个实验室制作一个脚本,该脚本将清点那里的所有服务器及其统计信息。我subprocess用来将 bash 命令的输出传递到我的 python 代码中的变量中。但是,我得到的输出让我感到沮丧。在一台计算机上,输出以下内容:
def codename():
"""
Determines the product name of the actual motherboard/machine that it is used on
:return: Returns the system's codename
"""
sysman = str(subprocess.check_output(('dmidecode', '-s', 'system-manufacturer'))).rstrip() + " "
sysprodname = str(subprocess.check_output(('dmidecode', '-s', 'system-product-name'))).rstrip() + " "
sysver = str(subprocess.check_output(('dmidecode', '-s', 'system-version'))).rstrip()
# Checks for incomplete information
sysinfo = [sysman, sysprodname, sysver]
for i in range(len(sysinfo)):
if not any(c.isalpha for c in sysinfo[i]):
sysinfo[i] = ""
sysname = sysman + sysprodname + sysver
if sysname is not None:
return str(sysname)
else:
return "---"
当仅通过调用codename()Python 2(准确地说是 2.7)调用时,将返回:
innotek GmbH VirtualBox 1.2
在 Python 3 (3.6.5) 中运行它时返回:
b'innotek' GmbH\n' b'VirtualBox\n' b'1.2\n'.
我开始认为这是一个特定于 python 的问题,但是在我编写代码的另一台机器(openSUSE Tumbleweed)上,python --version返回:
Python 3.6.5
并且python3 --version还返回:
Python 3.6.5.
但是,当我运行它时,python script.py我得到了格式良好的字符串,而当我运行它时python3 script.py,我得到了字符串文字版本。有人可以帮我理解一下吗?