解码subprocess.Popen并存储在文件中

时间:2015-05-28 12:34:10

标签: python subprocess decode encode popen

我为pyLoad编写了一个脚本/插件。

基本上它使用参数执行FileBot。

我要做的是获取输出并将其存储到pyLoad日志文件中。

到目前为止一切顺利。它一直工作到需要解码单个字符的那一点。

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 5: ordinal not in range(128)

我不知道该怎么做。 我希望你们能帮忙。

    try:
        if self.getConfig('output_to_log') is True:
            log = open('Logs/log.txt', 'a')
            subprocess.Popen(args, stdout=log, stderr=log, bufsize=-1)

提前致谢

[编辑]

28.05.2015 12:34:06 DEBUG     FileBot-Hook: MKV-Checkup (package_extracted)
28.05.2015 12:34:06 DEBUG     Hier sind keine Archive
28.05.2015 12:34:06 INFO      FileBot: executed
28.05.2015 12:34:06 INFO      FileBot: cleaning
Locking /usr/share/filebot/data/logs/amc.log
Done ヾ(@⌒ー⌒@)ノ
Parameter: exec = cd / && ./filebot.sh "{file}"
Parameter: clean = y
Parameter: skipExtract = y
Parameter: reportError = n
Parameter: storeReport = n
Parameter: artwork = n
Parameter: subtitles = de
Parameter: movieFormat = /mnt/HD/Medien/Movies/{n} ({y})/{n} ({y})
Parameter: seriesFormat = /mnt/HD/Medien/TV Shows/{n}/Season {s.pad(2)}/{n} - {s00e00} - {t}
Parameter: extras = n

所以我猜这个

Done ヾ(@⌒ー⌒@)ノ

导致问题

当我打开webgui上的loginterface以查看日志时 - 这是追溯

Traceback (most recent call last):
  File "/usr/share/pyload/module/lib/bottle.py", line 733, in _handle
    return route.call(**args)
  File "/usr/share/pyload/module/lib/bottle.py", line 1448, in wrapper
    rv = callback(*a, **ka)
  File "/usr/share/pyload/module/web/utils.py", line 113, in _view
    return func(*args, **kwargs)
  File "/usr/share/pyload/module/web/pyload_app.py", line 464, in logs
    [pre_processor])
  File "/usr/share/pyload/module/web/utils.py", line 30, in render_to_response
    return t.render(**args)
  File "/usr/share/pyload/module/lib/jinja2/environment.py", line 891, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/share/pyload/module/web/templates/Next/logs.html", line 1, in top-level template code
    {% extends 'Next/base.html' %}
  File "/usr/share/pyload/module/web/templates/Next/base.html", line 179, in top-level template code
    {% block content %}
  File "/usr/share/pyload/module/web/templates/Next/logs.html", line 30, in block "content"
    <tr><td class="logline">{{line.line}}</td><td>{{line.date}}</td><td class="loglevel">{{line.level}}</td><td>{{line.message}}</td></tr>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 5: ordinal not in range(128)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

        proc=subprocess.Popen(args, stdout=subprocess.PIPE)
        for line in proc.stdout:
            self.logInfo(line.decode('utf-8').rstrip('\r|\n'))
        proc.wait()