python:os.dup2,在pycharm中工作,但在pyinstaller中不工作
如您所见,这是一个简单的python代码,它将标准输出发送到一个文件 而且它很有魅力 但是,在使用pyinstaller创建exe文件后,它不起作用python:os.dup2,在pycharm中工作,但在pyinstaller中不工作,python,pycharm,pyinstaller,dup2,Python,Pycharm,Pyinstaller,Dup2,如您所见,这是一个简单的python代码,它将标准输出发送到一个文件 而且它很有魅力 但是,在使用pyinstaller创建exe文件后,它不起作用 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())
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)
操作系统是Windows10(1909)你能写信给标准输出吗?试着写下来检查一下。试着写出来并检查。我怀疑问题在于如何创建文件(所以pyinstaller配置)。嗨~ Giacomo Catenazzi。谢谢你的评论。它似乎写得很好
os.write(out\u fd,b'1\n')
os.dup2(out\u fd,sys.stdout.fileno())os.dup2(out\u fd,sys.stdout.fileno())
时,控制台屏幕上会显示print('test')
和os.write(sys.stdout.fileno(),b'2\n'
。
> 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
> pyinstaller stdout_dup2_test.py