【问题标题】:Execute a terminal command within a try/except block (Jupyter notebook)在 try/except 块中执行终端命令(Jupyter notebook)
【发布时间】:2022-11-12 01:31:25
【问题描述】:

我想使用 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)

我知道这行不通,但是有没有办法实现这个目标?

【问题讨论】:

    标签: python amazon-s3 jupyter-notebook


    【解决方案1】:

    subprocess.call 将返回返回码如果命令成功,它将为零,其他任何内容都表示错误

    所以

    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!!!")
    

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 2019-08-20
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 2018-06-01
      相关资源
      最近更新 更多