This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: General Windows stdout redirection not working
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Error in sys.excepthook on windows when redirecting output of the script
View: 9390
Assigned To: Nosy List: CrouZ, amaury.forgeotdarc, r.david.murray
Priority: normal Keywords:

Created on 2011-06-19 11:54 by CrouZ, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg138626 - (view) Author: (CrouZ) Date: 2011-06-19 11:54
Steps to repeat:
* Create the script foo.py consisting of the line: print("foo")
* Run: foo.py > bar

Behavior:
 2.7.2: The file bar is created but empty. Prints the following message on exit:

close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

 3.1.4: The file bar is created but empty. No printouts indicating that something went wrong.

Expected behavior:
 All: The file bar has the content foo.

Additional information:
 sys.stdout, and probably also sys.stderr, do not appear to be set up properly.
  2.7.2: stdout is set to a file but performing any operations on it throws the exception: IOError: [Errno 9] Bad file descriptor
  3.1.4: stdout is set to None

Workaround:
 All: python foo.py > bar

Used software:
 Windows 7 Ultimate SP1 x64 (6.1.7601)
 Python 2.7.2 and 3.1.4 installed with the msi-installer including Registering Extensions.
msg138630 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-06-19 14:38
The file association for .py is pythonw, which does exactly as you say, intentionally, so as to avoid problems with windows when running a GUI application.  My understanding is that this caters to the most common use case on Windows: double clicking a .py file launching a Python GUI application.  When running a python script in the console, use 'python', as in your example (or manually change your file association).
msg138634 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-06-19 15:28
"The file association for .py is pythonw"
Really? http://docs.python.org/using/windows.html#executing-scripts
msg138635 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-06-19 15:32
IMO the cause is actually the same as the one for issue9390, i.e. a bug in the Windows console.
msg138638 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-06-19 15:57
Ah, I should have known better than to rely on a memory instead of checking, since I don't use Windows much.  My apologies.
History
Date User Action Args
2022-04-11 14:57:18adminsetgithub: 56571
2011-06-19 15:59:51r.david.murraysetdependencies: - Error in sys.excepthook on windows when redirecting output of the script
superseder: Error in sys.excepthook on windows when redirecting output of the script
2011-06-19 15:57:09r.david.murraysetresolution: not a bug -> duplicate
dependencies: + Error in sys.excepthook on windows when redirecting output of the script
messages: + msg138638
2011-06-19 15:32:46amaury.forgeotdarcsetmessages: + msg138635
2011-06-19 15:28:34amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg138634
2011-06-19 14:38:53r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg138630

resolution: not a bug
stage: resolved
2011-06-19 11:54:44CrouZsettype: crash -> behavior
2011-06-19 11:54:28CrouZcreate