title: How to know what the error is when I use subprocess.run() in python with exception?
body: <p>I am converting bash code to python.</p>
  
  <p>I use mkdir of bash through subprocess.run() in python.</p>
  
  <p>In the following example, subprocess.run() raise an exception.</p>
  
  <p>However I could not check what the error is because I could not
  get an resultant object returned by subprocess.run().</p>
  
  <p>Are there any smart ways to know what the error was?
   Or should not I use 'try exception' here?</p>
  
  <pre><code>import sys
  import subprocess
  
  directory = '/tmp/test_dir'
  options = ''
  
  try:
  
      result=subprocess.run(['mkdir', options, directory], check=True)
  
  except subprocess.CalledProcessError as ex:
  
      print("In this example, subprocess.run() above raise an exception CalledProcessError.")
      # print("I would like to check result.returncode = {0}. But it failed because object 'result' is not defined.".format(result.returncode))
  
  except Exception as ex:
  
      sys.stderr.write("This must not happen.")
      sys.exit(1)   
  </code></pre>
  
  <p>Thank you very much.</p>