开发者

How can I capture the output from a shell command in Python?

What I need to do is the following: in a Python script spawn, say the ls --colors=always / Linux command, and read its output. The important part of this is that I need the output to keep all the ANSI escape sequences (colors and such) to later translate these sequences into H开发者_如何学编程TML. I heard that Python pty module can do that, but I could not find a useful example of its usage in the Internet, and this module's documentation is not quite comprehensive. I'll appreciate if someone could guide me through the way of accomplishing this task.


import subprocess as sub

process = sub.Popen("ls --colors=always /", stdout=sub.PIPE, stderr=sub.PIPE)
output, errors = process.communicate()

Now all the data you want should be in output - including the ANSI escape sequences.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜