天天看點

Python 從 subprocess 運作的子程序中實時擷取輸出(shell 在 python 中執行)

需求

實作

1. os.system(cmd)

In [60]: path = "/hdfs_path/.../"

In [61]: cmd = ' /opt/tiger/.../bin/hadoop fs -du -s -h {} '.format(path)
    ...: res = os.system(cmd)
    ...:
# 如下,輸出隻是列印在螢幕上,沒辦法用變量存起來
0  0  /hdfs_path/.../

In [62]: res  # 為 0 僅表示指令運作成功,與指令的标準輸出無關
Out[62]: 0      

2. subprocess

def du_hdfs_file(path):

    from subprocess import PIPE, Popen

    def cmdline(command):
        """擷取标準輸出"""
        process = Popen(
            args=command,
            stdout=PIPE,
            shell=True
        )
        return process.communicate()[0]

    cmd = ' /opt/tiger/.../bin/hadoop fs -du -s -h {} '.format(path)
    # res = os.system(cmd)
    res = cmdline(cmd)
    return res

######################################
In [65]: res = du_hdfs_file(path)  # 标準輸出指派給 res

In [66]: res
Out[66]: '0  0  /hdfs_path/.../\n'

In [67]: res[0]
Out[67]: '0'      

參考

  1. ​​Assign output of os.system to a variable and prevent it from being displayed on the screen [duplicate]​​