python:os.dup2,在pycharm中有效,但在pyinstaller中无效

问题描述

    import os
    import sys
    
    if __name__ == '__main__':
        out_fd = os.open('out.txt',os.O_CREAT | os.O_WRONLY)
        os.dup2(out_fd,sys.stdout.fileno())
        print('test')
        os.close(out_fd)

如您所见,这是一个将stdout发送到文件的简单python代码

它可以在pycharm中使用。

但是,使用pyinstaller创建exe文件后,该功能不起作用。

> stdout_dup2_test.exe
Traceback (most recent call last):
  File "stdout_dup2_test.py",line 7,in <module>
OSError: [WinError 6] The handle is invalid
[6436] Failed to execute script stdout_dup2_test
Traceback (most recent call last):
  File "<string>",line 1,in <module>
OSError: [WinError 6] The handle is invalid
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
OSError: [WinError 6] The handle is invalid

我在python 3.7.9 + pyinstaller 4.0和python 3.6.9 + pyinstaller 3.6上进行了测试

> pyinstaller stdout_dup2_test.py

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)