如何将 Ruby 的 IO.popen 调用转换为 Python 的 subprocess.Popen 调用?

发布于 2024-08-26 10:01:09 字数 390 浏览 4 评论 0原文

我已经阅读了文档,并且在 REPL 和 Google 中尝试了很多东西,但我一生都无法理解 subprocess.Popen 在 Python 中工作。

以下是我正在使用的一些 Ruby 代码:

IO.popen("some-process") do |io|
  while(line = io.gets)
    # do whatever with line
  end
end

How do I translate this into Python using subprocess.Popen?

I've read the documentation and I've tried lots of things in the REPL, and Googled, but I can't for the life of me understand how subprocess.Popen works in Python.

Here is some Ruby code I am using:

IO.popen("some-process") do |io|
  while(line = io.gets)
    # do whatever with line
  end
end

How do I translate this into Python using subprocess.Popen?

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

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

发布评论

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

评论(2

撧情箌佬 2024-09-02 10:01:10

可能是 Python 中 Ruby 代码最简单的“近亲”:

>>> import subprocess
>>> io = subprocess.Popen('ls', stdout=subprocess.PIPE).stdout
>>> for line in io: print(line.strip())

Probably the simplest "close relative" of your Ruby code in Python:

>>> import subprocess
>>> io = subprocess.Popen('ls', stdout=subprocess.PIPE).stdout
>>> for line in io: print(line.strip())
乖乖兔^ω^ 2024-09-02 10:01:10
import subprocess

process = subprocess.Popen(['ls',], stdout=subprocess.PIPE)
print process.communicate()[0]
import subprocess

process = subprocess.Popen(['ls',], stdout=subprocess.PIPE)
print process.communicate()[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文