在try/except块中执行终端命令(Jupyter notebook)



我想使用AWS终端命令同步Jupyter笔记本的输出:

aws s3 sync <local_path> <s3://<bucket>/destination_path>

但是,如果可能的话,我想把这个命令放在try/except块中。下面是我要做的事情:

try:
!aws s3 sync <local_path> <s3://<bucket>/destination_path>
except Exception as e:
print(e)

我知道这行不通,但是有办法达到这个目标吗?

subprocess.call将返回return_code如果命令成功,该值将为零,其他值表示错误

cmd = "aws s3 sync <local_path> <s3://<bucket>/destination_path>"
if subprocess.call(cmd,shell=True):
print("There was an error with the command")
else:
print("Command Success!!!")

最新更新