当我仅使用脚本名称在 Windows 上运行 Python 脚本时无法重定向输出
它在 Windows 7(64 位)、带有 Win32 Python 扩展的 Python 2.6 上运行。
我有一个简单的脚本,只打印“hello world”。我可以使用 python hello.py 启动它。在这种情况下,我可以将输出重定向到文件。但是,如果我通过在命令行上输入 hello.py 并重定向输出来运行它,则会出现异常。
C:> python hello.py
hello world
C:> python hello.py >output
C:> type output
hello world
C:> hello.py
hello world
C:> hello.py >output
close failed in file object destructor:
Error in sys.excepthook:
Original exception was:
我想我是在升级到 Windows 7 后第一次遇到此错误。我记得它应该在 XP 中工作。我见过人们谈论这个错误 python-Bugs-1012692 |无法通过管道将输入传送到 python 程序。但那是很久以前的事了。而且它没有提到任何解决方案。
有人经历过这个吗?有人可以帮忙吗?
This is running on Windows 7 (64 bit), Python 2.6 with Win32 Extensions for Python.
I have a simple script that just print "hello world". I can launch it with python hello.py
. In this case I can redirect the output to a file. But if I run it by just typing hello.py
on the command line and redirect the output, I get an exception.
C:> python hello.py
hello world
C:> python hello.py >output
C:> type output
hello world
C:> hello.py
hello world
C:> hello.py >output
close failed in file object destructor:
Error in sys.excepthook:
Original exception was:
I think I first get this error after upgrading to Windows 7. I remember it should work in XP. I have seen people talking about this bug python-Bugs-1012692 | Can't pipe input to a python program. But that was long time ago. And it does not mention any solution.
Have anyone experienced this? Anyone can help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你是问这个吗?
它位于 Python 自述文件中。也许这个补丁就是您正在寻找的。
Are you asking about this?
It's in the Python README. Perhaps this patch is what you're looking for.
更新的答案
Microsoft 知识库问题(如果从文件关联启动,STDIN/STDOUT 重定向可能不起作用)可能正是这个问题。该页面包含下载 Win2000 修补程序的说明,但在更新的 Windows 版本上可能不需要这些说明。在修补程序之后(或者可能没有修补程序,具体取决于您的 Win 版本),需要手动编辑注册表。
您应该检查我提供的链接;无论如何,我总结如下:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
InheritConsoleHandles
的 DWORD 值并设置它到1
。据说就是这样。
UPDATED ANSWER
A Microsoft KB issue (STDIN/STDOUT Redirection May Not Work If Started from a File Association) may be exactly this issue. The page has instructions for downloading a Win2000 hotfix, but that might not be needed on more recent Windows versions. After the hotfix (or possibly without it, depending on your Win version), a manual registry edit is needed.
You should check the link I provided; in any case, I summarize here:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
InheritConsoleHandles
and set it to1
.That's it, supposedly.
如果您尝试从命令提示符重定向 stdout 和 stderr,
请参阅http://support.microsoft.com/kb/110930
If you are trying to redirect stdout and stderr from the command prompt,
see http://support.microsoft.com/kb/110930
我不知道这个问题,但我有一个想法来解决它。您是否考虑过添加命令行选项(如
-o
)来指定捕获输出的输出文件?I am not aware of the issue, but I have an idea to work around it. Have you thought about adding a command-line option (like
-o
) to specify an output file that will capture the output?