What are exactly the standard streams if there's no terminal/console window open for the python interpreter?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor
--
Chapters
00:00 Question
02:03 Accepted answer (Score 2)
04:06 Thank you
--
Full question
https://stackoverflow.com/questions/4437...
Question links:
[image]: https://i.stack.imgur.com/XXyTJ.png
[pythonw.exe or python.exe?]: https://stackoverflow.com/questions/9705...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #linux #windows #unix #terminal
#avk47
ACCEPTED ANSWER
Score 2
As others mentioned, the question is not Python-specific. When a process is spawned, it's the parent's duty to set the child's file descriptors (or to allow them to be inherited).
So, this is not even OS-specific, but actually application specific. For example, you might have a different anser for Gnome and KDE. Or when executing the file from within Windows Explorer or 7-Zip (I think; I know how this works on Unix, not so sure on Windows). Different applications spawning the process might be making different arrangements.
On Linux, you can find out by running lsof on the python process, which will list the opened files and tell you where exactly your stdout is going.  Here's your code, changed to do just that:
#!/usr/bin/env python
# vi: ai sts=4 sw=4 et
import sys, os, pprint
import subprocess
f = open("/tmp/output.txt", "w")
for stream in (sys.stdout, sys.stdin, sys.stderr):
    print (stream, file=f)
    print ("STTY?", stream.isatty(), file=f)
    print ("fileno:", stream.fileno(), file=f)
    print ("name:", stream.name, file=f)
#    print (pprint.pprint(dir(stream)), file=f)
    print (file=f)
subprocess.call ("lsof -p %s" % os.getpid(), stdout = f, shell = True)
Only the last line is actually important. See the output for a normal run:
(...)
test.py 29722 xxx    0u   CHR  136,4      0t0      7 /dev/pts/4
test.py 29722 xxx    1u   CHR  136,4      0t0      7 /dev/pts/4
test.py 29722 xxx    2u   CHR  136,4      0t0      7 /dev/pts/4
And when redirecting the output to a file:
(...)
test.py 29728 xxx    0u   CHR  136,4      0t0       7 /dev/pts/4
test.py 29728 xxx    1w   REG   0,38        0 2070222 /tmp/asdf.txt
test.py 29728 xxx    2u   CHR  136,4      0t0       7 /dev/pts/4
See that the file #1 has changed? Same will happen when you run it from Unity, telling you where that is going.
I didn't exactly reproduce your problem, as my Gnome 3 file manager won't run scripts like that and I'd not look into it, but I'm curious to know where yours goes.