Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
58 views

Python3 Subprocess Overview Cheatsheet

The document provides an overview of using the subprocess module in Python. It discusses different ways to execute commands and get output, handle processes, and use subprocess for threading. It includes examples of calling external programs, getting process IDs, communicating with processes, and using subprocess in scripts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Python3 Subprocess Overview Cheatsheet

The document provides an overview of using the subprocess module in Python. It discusses different ways to execute commands and get output, handle processes, and use subprocess for threading. It includes examples of calling external programs, getting process IDs, communicating with processes, and using subprocess in scripts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python3 – Subprocess Overview

One-liner (Same Task) Migrate os to subprocess


• print(subprocess.check_output(['ls', '-1']).decode('utf-8')) • os: output = os.spawnlp(os.P_WAIT, 'cmd', “arg')
• print(''.join(map(chr, subprocess.check_output(['ls', '-1'])))) • subprocess: output = call(['cmd', 'arg'])

Waits (No Threading) • os: pid = os.spawnlp(os.P_NOWAIT, 'cmd', 'arg')


subprocess.call(), subprocess.check_output(), • subprocess: pid = Popen(['cmd', 'arg']).pid
subprocess.getoutput(),
subprocess.Popen().stdout.readlines() Environment Variables
Threading proc = subprocess.Popen(['echo',
subprocess.Popen() os.environ['MY_ENV_VAR']])
#Windows only (threading)
DETACHED_PROCESS = 0x00000008 proc = subprocess.Popen('echo '$MY_ENV_VAR'',
subprocess.Popen([sys.executable, 'ls'], env=environ, shell=True)
creationflags=DETACHED_PROCESS).pid
#BSD only (threading) proc = subprocess.Popen(['echo',
pid = subprocess.Popen([sys.executable, 'ls'], os.path.expandvars('$MY_ENV_VAR')])
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE) • newenv = os.environ.copy()
• newenv['MY_ENV_VAR'] = 'value'
Open Program or Execute Code
subprocess.call(['EXCUTABLE']) # no threading Example
import subprocess
Get PID proc = subprocess.Popen('mousepad')
pid = subprocess.Popen(['ls', '-1'], shell=True, print(proc) # <subprocess.Popen object at
stdout=subprocess.PIPE, 0x7f3240609d30>
stderr=subprocess.STDOUT).stdout.read() pid = proc.pid # 13731
print(proc.poll()) # None
print(pid) # <subprocess.Popen object at 0x7f32405e80b8> proc.kill()
print(proc.poll()) # 0
type(pid) # subprocess.Popen proc = subprocess.Popen('mousepad')
pid = proc.pid
# .readline() reads one stdout line at a time try:
# .readlines() reads all stdout at once outs, errs = proc.communicate(timeout=15)
except TimeoutExpired: # when timer expires
Usage proc.kill() # close mousepad
x = subprocess.check_output(['cmd', 'arg1']) outs, errs = proc.communicate()
# x = b'Desktop\nDocuments\nDownloads\n'
z = x.decode('utf-8')) # plain text (str) # Example Lines
• proc.communicate(input='', timeout=int)
subprocess.getoutput('ls -l') # one str param • Return Code: p_status = proc.wait()
# Output = 'String\nString\nString'
• Send a Kill Signal: proc.send_signal(SIG)
Tricks • Stop the Process: proc.terminate()
shlex.split('ls -l') # output: ['ls', '-l'] • Wait for the Process: proc.wait()
subprocess.check_output(shlex.split('ls -l'))
Scripted Example
Shell2Python #!/usr/bin/env python3
Shell: output=`dmesg | grep hda` import subprocess, sys
Python: from subprocess import * cmd = 'netstat -p --tcp'
p1 = Popen(['dmesg'], stdout=PIPE) p = subprocess.Popen(cmd, shell=True,
p2 = Popen(['grep', 'hda'], stdin=p1.stdout, stdout=PIPE) stderr=subprocess.PIPE)
p1.stdout.close() while True: # displaying output immediately
# allow p1 to receive a SIGPIPE if p2 exits out = p.stderr.read(1)
output = p2.communicate()[0] if out == '' and p.poll() != None:
Python: break # exit after netstat closes
output = check_output('dmesg | grep hda', shell=True) if out != '': # release output
sys.stdout.write(out)
sys.stdout.flush()

Created by Devyn Collier Johnson <DevynCJohnson@Gmail.com> (2015 v2) More cheatsheets at DCJTech.info

You might also like