Home > Net >  Execute a terminal command within a try/except block (Jupyter notebook)
Execute a terminal command within a try/except block (Jupyter notebook)

Time:11-12

I would like to sync output from a Jupyter notebook using the the AWS terminal command:

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

However, I want to fit this command within a try/except block if possible. Something like the following is what I am trying to do:

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

I know that this does not work, but is there a way to meet this goal?

CodePudding user response:

subprocess.call will return the return_code which will be zero if the command was successful, and anything else indicates an error

so

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