Skip to content

Python scripting

library & tools

python subprocess library

basic usage

subprocess.run(["ls", -l])
  • .run() :

    subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None,  
    capture_output=False, shell=False, cwd=None, timeout=None, check=False,   
    encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)
    

  • .popen() : the subprocess is managed by popen class structure

class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, 
close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, 
restore_signals=True, start_new_session=False, pass_fds=(), *, group=None, extra_groups=None, user=None, 
umask=-1, encoding=None, errors=None, text=None, pipesize=-1, process_group=None)

### usage
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
  • .communicate()

return (stdout_data, stderr_data)

Popen.communicate(input = None, timeout = None)

### usage
proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()

PBD

a tool to add breakpoint and execute code

python -m pdb myscript.py
  • b: set a breakpoint
  • c: continue debugging until you hit a breakpoint
  • s: step through the code
  • n: to go to next line of code
  • l: list source code for the current file (default: 11 lines including the line being executed)
  • u: navigate up a stack frame
  • d: navigate down a stack frame
  • p: to print the value of an expression in the current context

To set breakpoint in code, simply adding breakpoint() will do the work for you

docs