[英]argument errors when using using Python to access to aws ecr
here is the function:这是功能:
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)
I'm trying to use this function to get aws erc get-login first, then use that returned login command to login to aws erc.我正在尝试使用此函数先获取 aws erc get-login,然后使用返回的登录命令登录到 aws erc。 here is my codes:
这是我的代码:
result = sh('aws', 'ecr', 'get-login', '--no-include-email', read_output=True)
re = result.split()
sh(re)
then I get error:然后我得到错误:
command_text = " ".join(command)
TypeError: sequence item 0: expected str instance, list found
I think the sh
function expect arguments something like `('docker', 'login', '-u', 'AWS', '-p'...), but how can I achieve this?我认为
sh
函数需要像`('docker', 'login', '-u', 'AWS', '-p'...) 这样的参数,但是我该如何实现呢?
You can use *
to unpack list/tuple and function get it as many arguments您可以使用
*
来解压列表/元组,并通过函数获取尽可能多的参数
sh( *re )
Or you can remove *
from *command
in definiton或者您可以在定义中从
*command
中删除*
def sh(command, ...)
and then you can send it only as list/tuple然后您只能将其作为列表/元组发送
sh( re )
but you can also check if command
is list
or string
但您也可以检查
command
是list
还是string
if isinstance(command, str):
command_text = command
elif isinstance(command, list, tuple):
command_text = " ".join(command)
so then you can send it directly as one string.所以你可以直接将它作为一个字符串发送。
sh( 'aws ecr get-login --no-include-email' )
or list with strings或带字符串的列表
sh( ['aws', 'ecr', 'get-login', '--no-include-email'] )
BTW: Similar way works **
with dictionary and named arguments顺便说一句:类似的方式适用于字典和命名参数
**
def fun(a=0, b=0, c=0):
print('a:', a)
print('b:', b)
print('c:', c)
data = {'b':2}
fun(**data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.