Shell Interaction in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 19: Line 19:


=Execute an O/S Command=
=Execute an O/S Command=
{{External|https://janakiev.com/blog/python-shell-commands/}}
{{External|https://amoffat.github.io/sh/}}
==Execute an O/S Command with the <tt>subprocess</tt> Module==
==Execute an O/S Command with the <tt>subprocess</tt> Module==
{{Internal|Python_Module_subprocess#Execute_an_O.2FS_Command|Execute an O/S Command with the <tt>subprocess</tt> Module}}
{{Internal|Python_Module_subprocess#Execute_an_O.2FS_Command|Execute an O/S Command with the <tt>subprocess</tt> Module}}
==Other Methods==
<font color=darkkhaki>TO PROCESS:
* https://janakiev.com/blog/python-shell-commands/
* https://amoffat.github.io/sh/
</font>


=Organizatorium=
=Organizatorium=

Revision as of 21:29, 2 March 2022

Internal

Command Line Argument Processing

sys.argv is the list containing arguments as strings. On the position 0 is the full path of the script being executed.

import sys

def main():
  print(f"Arguments count: {len(sys.argv)}")
  for i, arg in enumerate(sys.argv):
    print(f"Argument {i:>6}: {arg}")

if __name__ == '__main__':
  main()

Execute an O/S Command

Execute an O/S Command with the subprocess Module

Execute an O/S Command with the subprocess Module

Other Methods

TO PROCESS:

Organizatorium

from shutil import which

commands={
  "aws":                "awscli",
  "aws-login":          "aws-login",
  "aws-eks-configure":  "aws-eks-configure"
}
for c in commands:
  print("{:20}{:7}".format(commands[c], "OK" if which(c) else "missing"))