Shell Interaction in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 15: Line 15:
print(f"Arguments count: {len(sys.argv)}")
print(f"Arguments count: {len(sys.argv)}")
for i, arg in enumerate(sys.argv):
for i, arg in enumerate(sys.argv):
   print(f"Argument {i:>6}: {arg}")
   print(f"Argument {i:>1}: {arg}")
</syntaxhighlight>
</syntaxhighlight>



Revision as of 22:58, 19 June 2022

Internal

Command Line Argument Processing

Standalone Python programs often needs to handle command line arguments.

sys.argv is the list containing arguments as strings.

The path of the script being executed is available on position 0.

import sys

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

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"))