Under Ubuntu Desktop (Unity) when a script marked as executable and then I click on the file I get pop-up window like the one here in the image:
在Ubuntu Desktop(Unity)下,当一个脚本被标记为可执行文件然后我点击该文件时,我会看到弹出窗口,如图中所示:
pyscript.py is an executable Python script file with a shebang: #!/usr/bin/python
where /usr/bin/python is the path to the Python interpreter. Since I'm not running this process in a terminal window, because I just clicked "Run", I thought initially there would be no standard streams for the process; As I experimented more, I realized all the standard streams are available:
pyscript.py是一个带有shebang的可执行Python脚本文件:#!/ usr / bin / python其中/ usr / bin / python是Python解释器的路径。由于我没有在终端窗口中运行此过程,因为我只是单击“运行”,我认为最初没有标准流进程;当我进行更多实验时,我意识到所有标准流都可用:
pyscript.py
pyscript.py
#!/usr/bin/python3
import sys, os
f = file=open("output.txt", "w")
print(sys.stdout, sys.stdin, sys.stderr, sep='\n', file=f)
output.txt
output.txt的
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
Which raises the question, there's no terminal window running in the background, what are sys.stdout, stdin and stderr connected to? Technically, running Python without console window under Windows with .pyw I get the same output.
提出问题,后台没有运行终端窗口,sys.stdout,stdin和stderr连接到什么?从技术上讲,在Windows下使用.pyw在没有控制台窗口的情况下运行Python我得到相同的输出。
Take a look at this question: pythonw.exe or python.exe?
看看这个问题:pythonw.exe或python.exe?
One answer states the following:
一个答案说明如下:
Standard streams
sys.stdin
,sys.stdout
andsys.stderr
are NOT available.标准流sys.stdin,sys.stdout和sys.stderr不可用。
The emphasis that standard streams are not available doesn't seem to be true when I test this with Python 2.7 in Windows 10 by clicking the .pyw file, though, in the Windows registry .pyw files are associated with Python 2.X which runs pythonw.exe in my machine.
当我通过单击.pyw文件在Windows 10中使用Python 2.7进行测试时,标准流不可用的重点似乎不正确,但是,在Windows注册表中.pyw文件与运行的Python 2.X相关联我机器上的pythonw.exe。
This question is a Unix/Windows jumble!
这个问题是Unix / Windows混乱!
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).
正如其他人提到的那样,问题不是特定于Python的。当一个进程产生时,父进程必须设置子进程的文件描述符(或允许它们被继承)。
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.
因此,这甚至不是特定于操作系统的,而是实际上特定于应用程序。例如,您可能对Gnome和KDE使用不同的anser。或者在Windows资源管理器或7-Zip中执行文件时(我想;我知道这在Unix上是如何工作的,在Windows上不太确定)。产生该过程的不同应用可能会做出不同的安排。
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:
在Linux上,您可以通过在python进程上运行lsof来找到它,它将列出打开的文件并告诉您stdout的确切位置。这是你的代码,改为做到这一点:
#!/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.
看到文件#1已更改?当你从Unity运行它时会发生同样的事情,告诉你它在哪里。
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.
我没有完全重现你的问题,因为我的Gnome 3文件管理器不会运行这样的脚本而且我不会调查它,但我很想知道你的去向。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2017/06/05/d3466b1188dfd90078a5e8b1c448d1e5.html。