python3使用subprocess执行shell命令、导出docker镜像

 subprocess.run(cmd, stdout=subprocess.PIPE, encoding="utf8", shell=True, check=True).stdout

文档:https://www.runoob.com/w3cnote/python3-subprocess.html

from u_工具 import *
import subprocess
import platform

def is_linux_system():
    return 'linux' in platform.system().lower()

def is_windows_system():
    return 'windows' in platform.system().lower()


def shell(cmd, stdout=subprocess.PIPE, encoding="utf8", shell=True, check=True, **kwargs):
    return subprocess.run(cmd, stdout=stdout, encoding=encoding, shell=shell, check=check, **kwargs)\
                    .stdout


lines_str = shell("docker images | grep goharbor")
lines = stream(lines_str.split('\n')).filter(lambda i: i).collect()

for line in lines:
    name = line.split()[0]
    version = line.split()[1]
    print(f'''{name}:{version}''')
    # shell(f'''docker save {name}:{version} -o /a_soft/harbor/image/{name.split("/")[1]}:{version}.image''')

-- u_工具:https://github.com/hl-mio/u_util/tree/main/python3

猜你喜欢

转载自blog.csdn.net/u013595395/article/details/114752900