In [1]:
import sys
sys.version
sys.version_info


Out[1]:
sys.version_info(major=3, minor=6, micro=7, releaselevel='final', serial=0)

In [2]:
import subprocess

call example


In [3]:
# returns return code only
subprocess.call(["ls", "-lha"])


Out[3]:
0

note that no exception is raised if the underlying command errors:

bash-script-with-bad-syntax is a shell script with bad syntax.


In [7]:
subprocess.call(["./bash-script-with-bad-syntax"])


Out[7]:
127

call() example using shell=True


In [4]:
subprocess.call("ls -lha", shell=True)


Out[4]:
0

call example capture standard output and error


In [6]:
subprocess.call(["./bash-script-with-bad-syntax"])


Out[6]:
127

call example, force exception if called process causes error


In [8]:
# if there's no error in the underlying process,
# this is just the same as subprocess.call
subprocess.check_call(["ls","-lha"])


Out[8]:
0

In [32]:
# but unlike call, this throws a Called
subprocess.check_call(["./bash-script-with-bad-syntax"])


-------------------------------------------------------------------
CalledProcessError                Traceback (most recent call last)
<ipython-input-32-e87152a9901c> in <module>
      1 # but unlike call, this throws a Called
----> 2 subprocess.check_call(["./bash-script-with-bad-syntax"])

/usr/lib/python3.6/subprocess.py in check_call(*popenargs, **kwargs)
    289         if cmd is None:
    290             cmd = popenargs[0]
--> 291         raise CalledProcessError(retcode, cmd)
    292     return 0
    293 

CalledProcessError: Command '['./bash-script-with-bad-syntax']' returned non-zero exit status 127.

call example, capture stfout and stderr


In [29]:
import subprocess
import sys

# create two files to hold the output and errors, respectively
with open('out.txt','w+') as fout:
    with open('err.txt','w+') as ferr:
        out=subprocess.call(["./bash-script-with-bad-syntax"],stdout=fout,stderr=ferr)
        # reset file to read from it
        fout.seek(0)
            
        print('output:')
        print(fout.read())
        
        # reset file to read from it
        ferr.seek(0) 
        print('error:')
        print(ferr.read())


output:
total 48K
drwxrwxr-x  3 felipe felipe 4,0K Mar 28 23:28 .
drwxrwxr-x 48 felipe felipe 4,0K Mar 25 23:29 ..
-rwxrwxr-x  1 felipe felipe   41 Mar 28 21:20 bash-script-with-bad-syntax
-rw-rw-r--  1 felipe felipe  337 Mar 28 23:22 cmd.py
-rw-r--r--  1 felipe felipe  12K Mar 28 23:26 .cmd.py.swp
-rw-rw-r--  1 felipe felipe    0 Mar 28 23:28 err.txt
drwxrwxr-x  2 felipe felipe 4,0K Nov  3 19:32 .ipynb_checkpoints
-rw-rw-r--  1 felipe felipe  14K Mar 28 23:28 main.ipynb
-rw-rw-r--  1 felipe felipe    0 Mar 28 23:28 out.txt

error:
./bash-script-with-bad-syntax: line 4: foo: command not found

store output in variable


In [33]:
output = subprocess.check_output(["ls","-lha"],universal_newlines=True)
print(output)


total 56K
drwxrwxr-x  3 felipe felipe 4,0K Mar 28 23:28 .
drwxrwxr-x 48 felipe felipe 4,0K Mar 25 23:29 ..
-rwxrwxr-x  1 felipe felipe   41 Mar 28 21:20 bash-script-with-bad-syntax
-rw-rw-r--  1 felipe felipe  337 Mar 28 23:22 cmd.py
-rw-r--r--  1 felipe felipe  12K Mar 28 23:26 .cmd.py.swp
-rw-rw-r--  1 felipe felipe   62 Mar 28 23:28 err.txt
drwxrwxr-x  2 felipe felipe 4,0K Nov  3 19:32 .ipynb_checkpoints
-rw-rw-r--  1 felipe felipe  13K Mar 28 23:28 main.ipynb
-rw-rw-r--  1 felipe felipe  522 Mar 28 23:28 out.txt

run() examples


In [34]:
cp = subprocess.run(["ls","-lha"])
cp


Out[34]:
CompletedProcess(args=['ls', '-lha'], returncode=0)

In [35]:
cp = subprocess.run(["ls -lha"],shell=True)
cp


Out[35]:
CompletedProcess(args=['ls -lha'], returncode=0)

In [51]:
cp = subprocess.run(["ls","-lha"], universal_newlines=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(cp)


CompletedProcess(args=['ls', '-lha'], returncode=0, stdout='total 60K\ndrwxrwxr-x  3 felipe felipe 4,0K Mar 28 23:32 .\ndrwxrwxr-x 48 felipe felipe 4,0K Mar 25 23:29 ..\n-rwxrwxr-x  1 felipe felipe   41 Mar 28 21:20 bash-script-with-bad-syntax\n-rw-rw-r--  1 felipe felipe  337 Mar 28 23:22 cmd.py\n-rw-r--r--  1 felipe felipe  12K Mar 28 23:26 .cmd.py.swp\n-rw-rw-r--  1 felipe felipe   62 Mar 28 23:28 err.txt\ndrwxrwxr-x  2 felipe felipe 4,0K Nov  3 19:32 .ipynb_checkpoints\n-rw-rw-r--  1 felipe felipe  18K Mar 28 23:32 main.ipynb\n-rw-rw-r--  1 felipe felipe  522 Mar 28 23:28 out.txt\n', stderr='')

In [37]:
cp = subprocess.run(["ls","foo bar"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
print(cp.stdout)
print(cp.stderr)


ls: cannot access 'foo bar': No such file or directory


In [52]:
subprocess.run(["ls","foo bar"], check=True)


-------------------------------------------------------------------
CalledProcessError                Traceback (most recent call last)
<ipython-input-52-f2920e44b5b2> in <module>
----> 1 subprocess.run(["ls","foo bar"], check=True)

/usr/lib/python3.6/subprocess.py in run(input, timeout, check, *popenargs, **kwargs)
    416         if check and retcode:
    417             raise CalledProcessError(retcode, process.args,
--> 418                                      output=stdout, stderr=stderr)
    419     return CompletedProcess(process.args, retcode, stdout, stderr)
    420 

CalledProcessError: Command '['ls', 'foo bar']' returned non-zero exit status 2.

In [38]:
try:
    cp = subprocess.run(["xxxx","foo bar"], universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except FileNotFoundError as e:
    print(e)


[Errno 2] No such file or directory: 'xxxx': 'xxxx'

popen


In [39]:
from subprocess import Popen

In [40]:
p = Popen(["ls","-lha"])
p.wait()


Out[40]:
0

In [41]:
p = Popen(["ls","-lha"], stdout=subprocess.PIPE, stderr= subprocess.PIPE, universal_newlines=True)

output, errors = p.communicate()

print(output)
print(errors)


total 56K
drwxrwxr-x  3 felipe felipe 4,0K Mar 28 23:28 .
drwxrwxr-x 48 felipe felipe 4,0K Mar 25 23:29 ..
-rwxrwxr-x  1 felipe felipe   41 Mar 28 21:20 bash-script-with-bad-syntax
-rw-rw-r--  1 felipe felipe  337 Mar 28 23:22 cmd.py
-rw-r--r--  1 felipe felipe  12K Mar 28 23:26 .cmd.py.swp
-rw-rw-r--  1 felipe felipe   62 Mar 28 23:28 err.txt
drwxrwxr-x  2 felipe felipe 4,0K Nov  3 19:32 .ipynb_checkpoints
-rw-rw-r--  1 felipe felipe  13K Mar 28 23:28 main.ipynb
-rw-rw-r--  1 felipe felipe  522 Mar 28 23:28 out.txt



In [42]:
path_to_output_file = '/tmp/myoutput.txt'

myoutput = open(path_to_output_file,'w+')

p = Popen(["ls","-lha"], stdout=myoutput, stderr= subprocess.PIPE, universal_newlines=True)

output, errors = p.communicate()

print(output)
print(errors)

with open(path_to_output_file,"r") as f:
    print(f.read())


None

total 56K
drwxrwxr-x  3 felipe felipe 4,0K Mar 28 23:28 .
drwxrwxr-x 48 felipe felipe 4,0K Mar 25 23:29 ..
-rwxrwxr-x  1 felipe felipe   41 Mar 28 21:20 bash-script-with-bad-syntax
-rw-rw-r--  1 felipe felipe  337 Mar 28 23:22 cmd.py
-rw-r--r--  1 felipe felipe  12K Mar 28 23:26 .cmd.py.swp
-rw-rw-r--  1 felipe felipe   62 Mar 28 23:28 err.txt
drwxrwxr-x  2 felipe felipe 4,0K Nov  3 19:32 .ipynb_checkpoints
-rw-rw-r--  1 felipe felipe  13K Mar 28 23:28 main.ipynb
-rw-rw-r--  1 felipe felipe  522 Mar 28 23:28 out.txt


In [43]:
path_to_output_file = '/tmp/myoutput.txt'

myoutput = open(path_to_output_file,'w+')

p = Popen(["ls","foo bar"], stdout=myoutput, stderr= myoutput, universal_newlines=True)

output, errors = p.communicate()

print(output)
print(errors)

with open(path_to_output_file,"r") as f:
    print(f.read())


None
None
ls: cannot access 'foo bar': No such file or directory

pipe commands together


In [44]:
from subprocess import Popen,PIPE

# this is equivalent to ls -lha | grep "ipynb"
p1 = Popen(["ls","-lha"], stdout=PIPE)
p2 = Popen(["grep", "ipynb"], stdin=p1.stdout, stdout=PIPE, universal_newlines=True)

p1.stdout.close()

output = p2.communicate()[0]

print(output)


drwxrwxr-x  2 felipe felipe 4,0K Nov  3 19:32 .ipynb_checkpoints
-rw-rw-r--  1 felipe felipe  13K Mar 28 23:28 main.ipynb

async


In [45]:
import asyncio

proc = await asyncio.create_subprocess_exec(
    'ls','-lha',
    stdout=asyncio.subprocess.PIPE,
    stderr=asyncio.subprocess.PIPE)


# if proc takes very long to complete,
# the CPUs are free to use cycles for 
# other processes
stdout, stderr = await proc.communicate()

print('[return code: '+ str(proc.returncode) +']')
if stdout:
    print('\n[stdout: ]\n'+str(stdout.decode()))
else:
    print('stdout is empty')
       
if stderr:
    print(f'\n[stderr]:\n'+str(stderr.decode()))
else:
    print('stderr is empty')


[return code: 0]

[stdout: ]
total 56K
drwxrwxr-x  3 felipe felipe 4,0K Mar 28 23:28 .
drwxrwxr-x 48 felipe felipe 4,0K Mar 25 23:29 ..
-rwxrwxr-x  1 felipe felipe   41 Mar 28 21:20 bash-script-with-bad-syntax
-rw-rw-r--  1 felipe felipe  337 Mar 28 23:22 cmd.py
-rw-r--r--  1 felipe felipe  12K Mar 28 23:26 .cmd.py.swp
-rw-rw-r--  1 felipe felipe   62 Mar 28 23:28 err.txt
drwxrwxr-x  2 felipe felipe 4,0K Nov  3 19:32 .ipynb_checkpoints
-rw-rw-r--  1 felipe felipe  13K Mar 28 23:28 main.ipynb
-rw-rw-r--  1 felipe felipe  522 Mar 28 23:28 out.txt

stderr is empty

In [ ]:


In [ ]: