从字符串中检索错误信息

时间:2018-08-10 22:30:43

标签: python-3.x subprocess

我有一个带有此示例错误的字符串

File "/home/mdk/Documents/PyPad/src/New289429.py", line 1 print("SDADADFDSFDSDSFS) ^ SyntaxError: EOL while scanning string literal 现在,这是给我该字符串的代码:

    def execute(self, command):
    """Executes a system command."""

    out, err = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    self.output = out
    self.error = err
    return self.error

我需要从该字符串中去除所有内容,但SyntaxError部分及其后的文本除外。像这样:SyntaxError: EOL while scanning string literal

我应该怎么做?

2 个答案:

答案 0 :(得分:1)

可能最简单的方法是从stderr中获取最后一行:

self.errow = err.split(os.linesep)[-2]

答案 1 :(得分:0)

要检索字符串的那部分

ConstraintLayout
相关问题