Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created March 11, 2014 15:41
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save s4553711/9488399 to your computer and use it in GitHub Desktop.
Save s4553711/9488399 to your computer and use it in GitHub Desktop.
Some example for subprocess.Popen exception example
#!/usr/bin/python
import subprocess
import os
import sys
#res = subprocess.Popen(['ls','-al','/ahome'],stdout=subprocess.PIPE,stderr=subprocess.PIPE);
#output,error = res.communicate()
#if res.returncode:
# #raise Exception(error)
# print "error>>>> ",res.returncode
#else:
# print "output>>>> ",output
try:
res = subprocess.Popen(['ls','-al','/home'],stdout=subprocess.PIPE,stderr=subprocess.PIPE);
#res = subprocess.Popen(['xls','-al','/home'],stdout=subprocess.PIPE);
output,error = res.communicate()
if output:
print "ret> ",res.returncode
print "OK> output ",output
if error:
print "ret> ",res.returncode
print "Error> error ",error.strip()
#except CalledProcessError as e:
# print "CalledError > ",e.returncode
# print "CalledError > ",e.output
except OSError as e:
print "OSError > ",e.errno
print "OSError > ",e.strerror
print "OSError > ",e.filename
except:
print "Error > ",sys.exc_info()[0]
@pashh
Copy link

pashh commented Jun 19, 2015

it looks good thank for hint

@youngminz
Copy link

Thank you!

@azherullahkhan
Copy link

Thank you

@SealedJoy
Copy link

thanks for sharing 👍 !

@AmirAmraei
Copy link

thanks and big like

@Pezhvak
Copy link

Pezhvak commented Dec 19, 2020

thanks for sharing

@basavyr
Copy link

basavyr commented May 23, 2021

Great! Appreciate the share 👍

@ANKITSG15
Copy link

so we can add wait() system call after res = subprocess.Popen ??

@ayushi-agrawal-gladstone

thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment