Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created March 11, 2014 15:41
Show Gist options
  • 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]
@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