Python将stdout作为列表获取

Python将stdout作为列表获取,python,linux,operating-system,Python,Linux,Operating System,这是我的密码: rows = subprocess.check_output("ls -1t | grep 'syslogdmz'", shell=True) 我得到的结果是两个文件名,但我不明白为什么它不把它们放在列表中。有办法吗 从文档中感谢您 “使用参数运行命令,并以字节字符串形式返回其输出。” 不确定您希望获得列表的原因。请参阅手册页 >>> import subprocess >>> help(subprocess.check_output) He

这是我的密码:

rows = subprocess.check_output("ls -1t | grep 'syslogdmz'", shell=True)
我得到的结果是两个文件名,但我不明白为什么它不把它们放在列表中。有办法吗

从文档中感谢您

“使用参数运行命令,并以字节字符串形式返回其输出。”


不确定您希望获得列表的原因。

请参阅手册页

>>> import subprocess
>>> help(subprocess.check_output)
Help on function check_output in module subprocess:

check_output(*popenargs, **kwargs)
    Run command with arguments and return its output as a byte string.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.

    The arguments are the same as for the Popen constructor.  Example:

    >>> check_output(["ls", "-l", "/dev/null"])
    'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'

    The stdout argument is not allowed as it is used internally.
    To capture standard error in the result, use stderr=STDOUT.

    >>> check_output(["/bin/sh", "-c",
    ...               "ls -l non_existent_file ; exit 0"],
    ...              stderr=STDOUT)
    'ls: non_existent_file: No such file or directory\n'

>>>
尝试使用
os.popen
获取列表中的输出。 或者也可以使用split()进入列表

x = os.popen('ls -1t | grep syslogdmz').readlines()
print x

您可能需要使用
os.popen

from os import popen
rows = popen("ls -1t | grep 'syslogdmz'","r").readlines()

rows
将在列表中包含结果。

您可以使用
rows.splitlines()
来获取字节字符串列表,而不是一个包含某些字符串的字符串。这似乎是可行的,难道您不知道如何避免在不必为每行重新排序的情况下获取\n吗。非常感谢。