I have a script named patchWidth.py
and it parses command line arguments with argparse
:
# read command line arguments -- the code is able to process multiple files
parser = argparse.ArgumentParser(description='angle simulation trajectories')
parser.add_argument('filenames', metavar='filename', type=str, nargs='+')
parser.add_argument('-vec', metavar='v', type=float, nargs=3)
Suppose this script is run with the following:
>>> python patchWidth.py file.dat -vec 0. 0. 1.
Is there a way to get this entire thing as a string in python? I would like to be able to print to the output file what command was run with what arguments.
Yes, you can use the sys module:
import sys
str(sys.argv) # arguments as string
Note that argv[0]
is the script name. For more information, take a look at the sys module documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With