In [1]:
import sys
sys.version
sys.version_info
Out[1]:
In [2]:
import subprocess
In [3]:
# returns return code only
subprocess.call(["ls", "-lha"])
Out[3]:
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]:
In [4]:
subprocess.call("ls -lha", shell=True)
Out[4]:
In [6]:
subprocess.call(["./bash-script-with-bad-syntax"])
Out[6]:
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]:
In [32]:
# but unlike call, this throws a Called
subprocess.check_call(["./bash-script-with-bad-syntax"])
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())
In [33]:
output = subprocess.check_output(["ls","-lha"],universal_newlines=True)
print(output)
In [34]:
cp = subprocess.run(["ls","-lha"])
cp
Out[34]:
In [35]:
cp = subprocess.run(["ls -lha"],shell=True)
cp
Out[35]:
In [51]:
cp = subprocess.run(["ls","-lha"], universal_newlines=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(cp)
In [37]:
cp = subprocess.run(["ls","foo bar"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
print(cp.stdout)
print(cp.stderr)
In [52]:
subprocess.run(["ls","foo bar"], check=True)
In [38]:
try:
cp = subprocess.run(["xxxx","foo bar"], universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except FileNotFoundError as e:
print(e)
In [39]:
from subprocess import Popen
In [40]:
p = Popen(["ls","-lha"])
p.wait()
Out[40]:
In [41]:
p = Popen(["ls","-lha"], stdout=subprocess.PIPE, stderr= subprocess.PIPE, universal_newlines=True)
output, errors = p.communicate()
print(output)
print(errors)
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())
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())
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)
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')
In [ ]:
In [ ]: