1

What my function does:

  1. creates a war file by processing the folder contents (/tmp/A, /tmp/B and so on)
  2. Does some file path and folder path manipulations to get the final version from the war file.
  3. store the file name in one variable and the version in another.
  4. Push the war file to the Repository using curl.

I'm using multiple try & except blocks to catch the exception for each action and looks very un-pythonic.

Is there an elegant and simple way to approach this ? thanks in advance.

import shutil import traceback import subprocess import os import glob def my_function(path_a, path_b, tmp_dir) try: <shutil.copy to the tmp dir> except: traceback.print_exc() try: war_process = subprocess.run([WAR GENERATION COMMAND], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(war_process.stdout.decode("utf-8")) except subprocess.CalledProcessError as e: exit_code = e.returncode stderror = e.stderr print(exit_code, stderror) print(war_process.stderr.decode("utf-8")) try: output_folder = os.path.join("/tmp/dir/work", FILE_PATH, ARTIFACT_DATE, FILE_WO_EXTENSION) except: traceback.print_exc() try: file_name = list(glob.glob(os.path.join(output_folder, "*.war"))) except: traceback.print_exc() try: file_path = os.path.join(output_folder, file_name) except: traceback.print_exc() try: os.rename(file_path, file_path.split('war')[0] + ".tgz") except: traceback.print_exc() try: file_version = os.path.basename(file_path) except: traceback.print_exc() cmd = "curl -u username -T ....)" try: curl_output = subprocess.run([cmd], shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(curl_output.stdout.decode("utf-8")) except subprocess.CalledProcessError as er: print(proc_c.stderr.decode("utf-8")) exit_c = er.returncode std = er.stderr print(exit_c, std) 
6
  • you are missing a " in your code at cmd = "curl -u username -T .....)" Commented Oct 1, 2021 at 7:16
  • A lot of these try blocks are pretty unnecessary, an uncaught exception will already print the traceback in the console. Why are you doing this in the first place and why are you creating a new block for every line? Commented Oct 1, 2021 at 7:17
  • It's a typo, while sharing the code. Commented Oct 1, 2021 at 7:17
  • @Andrew-Harelson : I'm trying to get the output of the folders that's available once the war command runs, so the multiple try blocks, else I dont see any tracebacks :( Commented Oct 1, 2021 at 7:24
  • @ChelMS Ah I understand now, the answers below give good advice Commented Oct 1, 2021 at 7:26

3 Answers 3

5

You can write try once, then handle all the exceptions later:

try: output_folder = os.path.join("/tmp/dir/work", FILE_PATH, ARTIFACT_DATE, FILE_WO_EXTENSION) file_name = list(glob.glob(os.path.join(output_folder, "*.war"))) file_path = os.path.join(output_folder, file_name) os.rename(file_path, file_path.split('war')[0] + ".tgz") except FooException: print('foo') except BarException: print('bar') 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input :) , yes, I did try this approach but didn't workout.. Looks like I messed it up..
3

First of all never use bare except in your code. Read bullets 6 to 11 in PEP8:Programming Recommendations.

My suggestion is to use this code instead:

def my_function(path_a, path_b, tmp_dir) try: <shutil.copy to the tmp dir> except: traceback.print_exc() try: war_process = subprocess.run([WAR GENERATION COMMAND], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(war_process.stdout.decode("utf-8")) output_folder = os.path.join("/tmp/dir/work", FILE_PATH, ARTIFACT_DATE, FILE_WO_EXTENSION) file_name = list(glob.glob(os.path.join(output_folder, "*.war"))) file_path = os.path.join(output_folder, file_name) os.rename(file_path, file_path.split('war')[0] + ".tgz") file_version = os.path.basename(file_path) except subprocess.CalledProcessError as e: exit_code = e.returncode stderror = e.stderr print(exit_code, stderror) print(war_process.stderr.decode("utf-8")) except Exception as e: print(f'The program caught an exception {e}') traceback.print_exc() cmd = "curl -u username -T ....)" try: curl_output = subprocess.run([cmd], shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(curl_output.stdout.decode("utf-8")) except subprocess.CalledProcessError as er: print(proc_c.stderr.decode("utf-8")) exit_c = er.returncode std = er.stderr print(exit_c, std) 

The second and the third try/except blocks must stay separated because both catch the same exception.

Also, if any of the blocks you created here catch a specific exception in this list, you should behave them like you behave the subprocess.CalledProcessError .

Best practice is to write one try block with multiple excepts in which each except block catches a specific exception.

3 Comments

Really appreciate the explanation and the information shared.
@ChelMS My pleasure. One more suggestion is if you're into Python and wanna be a Python Developer, make sure to read the entire PEP8. It's really useful!
Sure, currently exploring Python world, thanks..
2

You don't need to put a try/except block after every statement. It would be better to put multiple statements in a try/except block

def my_function(path_a, path_b, tmp_dir) try: <shutil.copy to the tmp dir> war_process = subprocess.run([WAR GENERATION COMMAND], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(war_process.stdout.decode("utf-8")) except subprocess.CalledProcessError as e: exit_code = e.returncode stderror = e.stderr print(exit_code, stderror) print(war_process.stderr.decode("utf-8")) try: output_folder = os.path.join("/tmp/dir/work", FILE_PATH, ARTIFACT_DATE, FILE_WO_EXTENSION) file_name = list(glob.glob(os.path.join(output_folder, "*.war"))) file_path = os.path.join(output_folder, file_name) os.rename(file_path, file_path.split('war')[0] + ".tgz") file_version = os.path.basename(file_path) except: traceback.print_exc() cmd = "curl -u username -T ....)" try: curl_output = subprocess.run([cmd], shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(curl_output.stdout.decode("utf-8")) except subprocess.CalledProcessError as er: print(proc_c.stderr.decode("utf-8")) exit_c = er.returncode std = er.stderr print(exit_c, std) ``` 

1 Comment

I used try once and multiple exceptions, but didn't work.. Will try this out, thanks a lot !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.