Python Forum
Get anything before arguments - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Get anything before arguments (/thread-33697.html)



Get anything before arguments - Robotguy - May-17-2021

I understand how to retrieve arguments from any user input from powershell. However, I want to get what exactly was typed before arguments. For example, for the command entered by user: py myscript.py, I should print/retrieve py.

Similarly for user input python myscript.py, I should print/get python ?


RE: Get anything before arguments - BashBedlam - May-18-2021

This should do the trick.

import psutil
import os
my_process = psutil.Process (os.getpid())
print (my_process.cmdline())



RE: Get anything before arguments - Robotguy - May-18-2021

Thanks for your reply. This prints /path/to/Python37-32/python.exe or /path/to/Python38-32/python.exe when I use python and py in command line, respectively. But that still doesn't tell whether the user entered python or py?

(May-18-2021, 02:01 AM)BashBedlam Wrote: This should do the trick.

import psutil
import os
my_process = psutil.Process (os.getpid())
print (my_process.cmdline())



RE: Get anything before arguments - BashBedlam - May-19-2021

Sorry Sad On Linux Mint I get :

Output:
['python3', 'tester.py', 'arg1', 'arg2']