开发者

How do I translate Ruby's IO.popen calls into Python's subprocess.Popen calls?

开发者 https://www.devze.com 2022-12-25 01:26 出处:网络
I\'ve read the documentation and I\'ve tried lots of things in the REPL, 开发者_JAVA百科and Googled, but I can\'t for the life of me understand how subprocess.Popen works in Python.

I've read the documentation and I've tried lots of things in the REPL, 开发者_JAVA百科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?


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())


import subprocess

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号