使用Python访问AWS ECR时出现参数错误

这是函数:

def sh(*command,read_output=False,**kwargs):
    command_text = " ".join(command)
    print(f"\t> {command_text}")
    try:
        if read_output:
            return check_output(command,**kwargs).decode("utf8")
        else:
            check_call(command,**kwargs)
    except CalledProcessError as failure:
        print(
            f'ERROR: "{command_text}" command reported failure! Return code {failure.returncode}.'
        )
        sys.exit(failure.returncode)

我正在尝试使用此功能先获取erc erc get-login,然后使用返回的login命令登录到aws erc。这是我的代码:

result = sh('aws','ecr','get-login','--no-include-email',read_output=True)
re = result.split()
sh(re)

然后我得到了错误:

command_text = " ".join(command)
TypeError: sequence item 0: expected str instance,list found

我认为sh函数期望使用类似'('docker','login','-u','AWS','-p'...)的参数,但是如何实现呢? ?

jenny013 回答:使用Python访问AWS ECR时出现参数错误

您可以使用*来解包列表/元组并使用函数获取尽可能多的参数

sh( *re )

或者您也可以从*中删除*command

def sh(command,...)

然后您只能将其作为列表/元组发送

sh( re )

但是您也可以检查commandlist还是string

if isinstance(command,str): 
    command_text = command 
elif isinstance(command,list,tuple): 
    command_text = " ".join(command)

因此您可以直接将其作为一个字符串发送。

sh( 'aws ecr get-login --no-include-email' )

或带有字符串的列表

sh( ['aws','ecr','get-login','--no-include-email'] )

顺便说一句:**与字典和命名参数类似的工作方式

def fun(a=0,b=0,c=0):
    print('a:',a)
    print('b:',b)
    print('c:',c)

data = {'b':2}

fun(**data)
本文链接:https://www.f2er.com/3004700.html

大家都在问