当我在Windows上使用脚本名称运行Python脚本时,无法重定向输出 [英] Cannot redirect output when I run Python script on Windows using just script's name

查看:644
本文介绍了当我在Windows上使用脚本名称运行Python脚本时,无法重定向输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是运行在Windows 7(64位),Python 2.6与Win32扩展为Python。



我有一个简单的脚本只打印hello world。我可以用 python hello.py 启动它。在这种情况下,我可以将输出重定向到一个文件。但是如果我通过在命令行上输入 hello.py 并重定向输出来运行它,我会得到一个异常。

  C:> python hello.py 
hello world

C:> python hello.py>输出

C:>类型输出
hello world

C:> hello.py
hello world

C:> hello.py>输出
在文件对象析构函数中关闭失败:
sys.excepthook中的错误:

原始异常为:

我想我升级到Windows 7后首先得到这个错误。我记得它应该在XP中工作。我看到有人在讨论这个错误 python-Bugs- 1012692 |不能管道输入到python程序。但这是很久以前。它没有提及任何解决方案。



有没有人遇到过这个?



解决方案

Windows:当使用文件类型
在命令行上执行Python脚本时(即从script.py而不是python script.py开始),
重定向可能无法工作,除非您设置了特定的注册表键。请参阅
知识库文章如果从文件关联启动,STDIN / STDOUT重定向可能无法正常工作< a>。



它在Python README中。也许这个补丁是你要找的。

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?

解决方案

Are you asking about this?

Windows: When executing Python scripts on the command line using file type associations (i.e. starting "script.py" instead of "python script.py"), redirects may not work unless you set a specific registry key. See the Knowledge Base article STDIN/STDOUT Redirection May Not Work If Started from a File Association.

It's in the Python README. Perhaps this patch is what you're looking for.

这篇关于当我在Windows上使用脚本名称运行Python脚本时,无法重定向输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆