1

How to get the run command in log file in Python ?

I had run the below command in command line and want to print the same command in log file so i can use that command from log file and can run it again.

Example: python checkcelllist -c celllist -s ronak.lib

it should print above command in log file.

python checkcelllist -c celllist -s ronak.lib

1 Answer 1

3

You can dump the contents of sys.argv to the file:

print >> logfile, ' '.join(sys.argv)

Note that this doesn't include the interpreter (python), only the script name and arguments.

If your arguments can contain spaces:

print >> logfile, ' '.join([(repr(arg) if ' ' in arg else arg) for arg in sys.argv])
Sign up to request clarification or add additional context in comments.

Comments

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.