Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot redirect output when I run Python script on Windows using just script's name

Tags:

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?

like image 740
Wai Yip Tung Avatar asked Jun 10 '10 21:06

Wai Yip Tung


People also ask

How do I run a Python file named text py in CMD?

Using the python Command To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do I redirect the output of a file in Windows?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do you run a .py file in Windows 10 CMD?

Type dir and you should see the file Hello.py. To run the program, type python Hello.py and hit Enter. You should see the line Hello World!


2 Answers

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.

like image 119
S.Lott Avatar answered Sep 25 '22 15:09

S.Lott


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.

like image 23
tzot Avatar answered Sep 22 '22 15:09

tzot