23

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?

2
  • 1
    Are you aware that there are TWO output streams: stdout and stderr? Commented Jun 10, 2010 at 23:04
  • Yes. And I want to redirect the stdout output. Commented Jun 11, 2010 at 5:35

4 Answers 4

20

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's it. Just adding the InheritConsoleHandles registry value does it! Thanks a ton!!!
The link to the article is now dead. Here is an archive of the page: web.archive.org/web/20150403070207/http://support.microsoft.com/… Edit: According to the latest version of that page, the issue was fixed in a service pack for Windows XP.
8

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:

  • Open Registry Editor and locate the key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
  • Add a DWORD value named InheritConsoleHandles and set it to 1.

That's it, supposedly.

4 Comments

I do have it in the PATHEXT. That's how I usually run the script. Unfortunately I got the same error with or without entering the .py extension.
@Wai: check the “new and improved” version of my answer, and let me know if this works for you.
Oops. Didn't see that S.Lott had already provided the same link. Sorry :)
Yes, that was the issue. Thanks. It helped a lot!
1

If you are trying to redirect stdout and stderr from the command prompt, see http://support.microsoft.com/kb/110930

Comments

0

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?

1 Comment

Thanks for your suggestion. This will do as a last resort :) But it is not as effortless as just typing ">output" on demand. Plus I have a lot of scripts and it is not practical to change all of them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.