Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:os.dup2,在pycharm中工作,但在pyinstaller中不工作_Python_Pycharm_Pyinstaller_Dup2 - Fatal编程技术网

python:os.dup2,在pycharm中工作,但在pyinstaller中不工作

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())

如您所见,这是一个简单的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())
        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())PyCharm按应执行程序:使用stdout/stdin。但是,当您为windows构建时,您可以选择使用本机windows程序,因此没有控制台。我用pyinstaller创建的exe文件是作为控制台程序在“cmd.exe”中执行的。当注释掉
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