python 重定向 后面的代码不运行_当我在Windows上使用脚本名称运行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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值