2

I have a python Script (Ex : myscript.py) with some Options which i can launch from my terminal :

myscript -n name - a age

which will print in my terminal :

10/15/2021 10:47:57 AM [INFO] # Script launched
10/15/2021 10:47:58 AM [INFO] # Your Name : name, Your age : age

How can modify my script, in which way it will log also the command launched at the beginning, like add logging.info("the command launched : {}.format(string_of_command)") :

10/15/2021 10:47:57 AM [INFO] # Script launched
10/15/2021 10:47:57 AM [INFO] # command used : myscript -n name - a age 
10/15/2021 10:47:59 AM [INFO] # Your Name : name, Your age : age
2
  • 1
    "I want to modify my script" -> then do it, we dont know your code Commented Oct 15, 2021 at 9:12
  • Okay, and what exactly is preventing you from solving the problem yourself? What have you tried thus far? For example, did you try putting python get command line into a search engine? What happened when you did that? Commented Oct 15, 2021 at 9:55

1 Answer 1

4

Maybe you can use the argv variable of sys module using the code like below

print(f"{' '.join(sys.argv)}")

So in your case it would be like this

string_of_command = f"{' '.join(sys.argv)}"
logging.info("the command launched : {}.format(string_of_command)")
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the hint, it gives the full path of the script.py also and options used, So I wil use some regex to keep only the useful part !
Can you try this one - os.path.basename(sys.argv[0]). And please accept the answer if this helps
the first answer was very helpful already accepted your answer ;) thanks arnaud :)
Ah sorry , don't have the green tick yet !
cool, thanks. cheers
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.