在 micropython 中重定向 Stderr?

问题描述 投票:0回答:3

使用 micro python,我尝试将 stderr 重定向到文件,但是我无权访问板上的运行命令,因此我无法执行任何 shell 重定向命令,例如

>> > |
。我很好奇是否有办法在 Micropython 中做到这一点。

我已经尝试过了

import sys

sys.stdout = open('out.txt', 'w')
sys.stderr = sys.stdout

然而

https://forum.micropython.org/viewtopic.php?t=2091<- this discussion mentions that it is not a method supported. Im curious if y'all have any ideas. Most my googling leads itself to just python instead of micropython.

但出现错误,指出模块没有名为 stdout 的属性 谢谢

python embedded-linux micropython openmv
3个回答
3
投票

这是我一直用来将控制台输出记录到文件的脚本

import io, os

class logToFile(io.IOBase):
    def __init__(self):
        pass

    def write(self, data):
        with open("logfile.txt", mode="a") as f:
            f.write(data)
        return len(data)
# Begin loging to file
os.dupterm(logToFile())

# Stop loging to file
os.dupterm(None)

1
投票

我还没有尝试过,但doc建议您如果使用

usys
而不是
sys
就可以做到这一点。
open()
实际上会调用 uio.open()


0
投票

在树莓派Pico W上遇到同样的问题,你找到解决方案了吗?

© www.soinside.com 2019 - 2024. All rights reserved.