【问题标题】:get output of subprocess whose output is constantly changing获取输出不断变化的子进程的输出
【发布时间】:2020-06-09 19:20:45
【问题描述】:

我想捕获archlinux的pacman包管理器的输出。这样做时,我想处理它的输出,但也想将其显示给用户。

这是我到目前为止所带来的:-

import subprocess as sb
import sys

lol = sb.Popen('sudo pacman -Syy',stdout=sb.PIPE,shell=True)

while True:
    l = lol.stdout.readline().strip()

    if not l and lol.poll() is not None:
        break

    sys.stdout.write(l.decode())
    sys.stdout.flush()

但它会打印:-

❯ python test.py
:: Synchronizing package databases...downloading core.db...downloading extra.db...downloading community.db...downloading multilib.db...done

这就是我想要的

:: Synchronizing package databases...
 core                                                                     135.2 KiB   160 KiB/s 00:01 [############################################################] 100%
 extra                                                                   1706.7 KiB   470 KiB/s 00:04 [############################################################] 100%
 community                                                                  4.9 MiB  1107 KiB/s 00:05 [############################################################] 100%
 multilib                                                                 161.2 KiB  3.09 MiB/s 00:00 [############################################################] 100%

done

【问题讨论】:

    标签: python subprocess pacman-package-manager


    【解决方案1】:

    这样的事情可能对你有用:

    import subprocess
    import sys
    
    # for py3 uncomment next line
    # basestring = str
    
    
    def cmd2args(cmd):
        """On linux we might need to split the command before executing...?
        """
        win32 = sys.platform == 'win32'
        if isinstance(cmd, basestring):
            if not win32 and cmd.startswith('cd '):
                return cmd
            else:
                return cmd if win32 else shlex.split(cmd)
        return cmd
    
    
    
    def echorun(cmd, curdir='.'):
        """Send stderr to terminal, but yield stdout one line at a time.
        """
        popen = subprocess.Popen(cmd2args(cmd),
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.STDOUT,
                                 cwd=curdir,
                                 shell=sys.platform == 'win32',
                                 universal_newlines=True)
        if popen.stdout is not None:
            for line in iter(popen.stdout.readline, ""):
                if sys.version_info.major == 3 and isinstance(line, str):
                    yield  line
                else:
                    yield line.decode()
    
        exitcode = popen.wait()
        if exitcode != 0:
            raise RuntimeError("Exitcode: %d" % exitcode)
    
    

    用法是:

        for line in echorun('sudo pacman -Syy', curdir):
            print(line)
    

    【讨论】:

      【解决方案2】:

      这就是你要找的吗?

      import subprocess as sb
      import sys
      
      lol = sb.Popen('sudo pacman -Syy',stdout=sb.PIPE,shell=True)
      
      while True:
          l = lol.stdout.readline().strip()
          if lol.poll() is not None:
              break
      
          sys.stdout.write(l.decode())
          sys.stdout.flush()
      
      print("done")
      

      输出:

      :: Synchronizing package databases...downloading core.db...downloading extra.db...downloading community.db...done
      

      【讨论】:

        猜你喜欢
        • 2017-05-19
        • 2011-09-08
        • 1970-01-01
        • 2016-03-14
        • 2015-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多