Ian Rehwinkel
Ian Rehwinkel

Reputation: 2615

Get all command line arguments in python

When I run python3 myscript.py foo bar 123 then sys.argv holds the value ['myscript.py', 'foo', 'bar', '123']. I'd like to access the name of the command itself (python3 in this case). How can I do that?

Upvotes: 1

Views: 1156

Answers (1)

Ian Rehwinkel
Ian Rehwinkel

Reputation: 2615

Using the psutil module (can be found here), it is possible to retrieve all command line arguments used:

psutil.Process().cmdline()

It returns a list of strings, including the command used to start the python interpreter (e.g. /usr/bin/python3).

Upvotes: 2

Related Questions