Python Forum
Get anything before arguments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get anything before arguments
#1
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 ?
Reply
#2
This should do the trick.

import psutil
import os
my_process = psutil.Process (os.getpid())
print (my_process.cmdline())
Reply
#3
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())
Reply
#4
Sorry Sad On Linux Mint I get :

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 3,856 Mar-02-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020