将命令或脚本通过管道输出到另一个 python 脚本

发布于 2024-10-02 18:01:53 字数 1193 浏览 6 评论 0原文

我相对于 python,我正在尝试编写一个 python 脚本,可以将命令或另一个脚本的输出通过管道传输到该脚本。

example command | python_sript.py

在 python 脚本中,我将基本上分析命令的输出并将其保存到文件中。

我以为我可以通过将 sys.stdin 重定向到 subprocess.PIPE 来做到这一点,但它不起作用。

sys.stdin = subprocess.PIPE

有人可以建议我应该如何处理这个问题吗?

另外,如果命令或脚本以比 python 脚本处理速度更快的速度传输数据,会发生什么情况。

注意:当我使用这个脚本时,

import sys
data = sys.stdin.readline()
print(data)

我得到这个

D:\>echo "test" | tee.py
The process tried to write to a nonexistent pipe.
Traceback (most recent call last):
  File "D:\Profiles\Administrator\My Documents\Workspace\tee.py", line 3, in <mo
dule>
    data = sys.stdin.readline()
AttributeError: 'NoneType' object has no attribute 'readline'

,当我使用这个脚本时,

import sys
data = input()
print(data)

我得到这个

D:\>echo "test" | tee.py
The process tried to write to a nonexistent pipe.
Traceback (most recent call last):
  File "D:\Profiles\Administrator\My Documents\Workspace\tee.py", line 3, in <mo
dule>
    data = input()
RuntimeError: input(): lost sys.stdin

I'm relatively to to python and I'm trying to write a python script to which one can pipe the output of a command or another script.

example command | python_sript.py

In python script I'll basically analyze the output of command and save it to file.

I thought I'll be able to do this with redirecting sys.stdin to subprocess.PIPE, but it didn't work.

sys.stdin = subprocess.PIPE

Can some one please suggest how should I approach this?

Also what would happen if command or script pipes data at faster rate then what python script can process.

Note: When I use this script

import sys
data = sys.stdin.readline()
print(data)

I get this

D:\>echo "test" | tee.py
The process tried to write to a nonexistent pipe.
Traceback (most recent call last):
  File "D:\Profiles\Administrator\My Documents\Workspace\tee.py", line 3, in <mo
dule>
    data = sys.stdin.readline()
AttributeError: 'NoneType' object has no attribute 'readline'

and when I use this

import sys
data = input()
print(data)

I get this

D:\>echo "test" | tee.py
The process tried to write to a nonexistent pipe.
Traceback (most recent call last):
  File "D:\Profiles\Administrator\My Documents\Workspace\tee.py", line 3, in <mo
dule>
    data = input()
RuntimeError: input(): lost sys.stdin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉睡月亮 2024-10-09 18:01:53

在(旧)Windows 上,当您使用管道时,默认情况下需要使用 python 可执行文件调用 python 脚本由于 NT 重定向错误

D:\> echo "test" | python tee.py

之后 sys.stdinraw_input()fileinput.input() 应该按预期工作。

On (old) Windows when you use pipes you need to call python scripts using python executable by default due to NT redirection bug:

D:\> echo "test" | python tee.py

After that sys.stdin, raw_input(), fileinput.input() should work as expected.

揽清风入怀 2024-10-09 18:01:53

您似乎对管道的工作原理有点不清楚。它们由操作系统处理,而不是由各个程序处理 - 因此,如果您编写一个旨在从管道获取输入数据的 Python 脚本,它应该正常读取 stdin ,并且将处理重定向你。数据产生的速度就会被消耗得很快;如果脚本消耗数据的速度比生成数据的速度慢,那么它们将存储在缓冲区中。

或者您是否正在尝试在两个 Python 脚本之间进行通信?如果是这样,有比通过 stdin 和 stdout 更好的方法。

You seem to be a bit unclear on how pipes work. They are handled by the OS, not the individual programs -- so if you write a Python script designed to take input data from a pipe, it should just read stdin as normal and the redirection will be handled for you. The data will be consumed as fast as they are generated; if the script consumes data more slowly than they are generated then they will be stored in a buffer.

Or are you trying to communicate between two Python scripts? If so there are better ways than through stdin and stdout.

夏末染殇 2024-10-09 18:01:53

问题 1(从管道读取)已由其他人回答:Just read stdin in python_script.py。操作系统通过管道处理重定向。

问题2(写入/读取速度)也已得到解答:操作系统管道是缓冲的,因此这里无需担心。

问题 3(堆栈跟踪):您的第一个程序对我来说运行良好。 - 你真的在这里给出完整的代码吗?

至于 subprocess.PIPE:如果您从正在运行的 Python 脚本中启动一个新命令,您实际上只想使用它,这样您就可以与子进程进行通信。因为你不这样做,所以它对你来说没有任何用处。 shell 级别的管道不构成父子进程。

Question 1 (reading from a pipe) has been answered by others: Just read stdin in python_script.py. The OS handles the redirection through pipes.

Question 2 (writing/reading speed) has also been answered: OS pipes are buffered, so nothing to be concerned about here.

Question 3 (the stack traces): Your first program runs fine with me. - Are you really giving the whole code here?

As for subprocess.PIPE: You really only want to use this, if you start a new command from within your running Python script, so you can communicate with the child process. As you are not doing this, it is of no use to you. Pipes on the shell level don't constitute parent-child processes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文