I want to find out the script inside - the exact command I used to run it. I tried the following:
#!/usr/bin/env python import sys, os print os.path.basename(sys.argv[0]), sys.argv[1:]
But he is losing information:
$ 1.py -1 dfd 'gf g' "df df" 1.py ['-1', 'dfd', 'gf g', 'df df']
You see - he already lost information about what is fading, I used double quotes, single quotes or no quotes at all - in the command.
Edit
Here is what I use. All arguments in my script have default values, and after parsing args with argparse :
args = parser.parse_args()
I will write them to the journal, or if there is a journal, rewrite them:
logName = "." + (os.path.splitext(os.path.basename(sys.argv[0])))[0] + ".json" if os.path.exists(logName): print "!!! I've found log", logName Args = bk_loads_json(logName) for arg in Args: exec('args.{0} = Args["{0}"]'.format(arg)) else: print "!!! the log of args is saved to", logName bk_saves_json(args.__dict__, logName)
defuns mentioned:
def bk_saves_json(myCustomDct, flNm): "Takes dict, and writes it to the file." FlNm = open(flNm, 'w') tmpJsn = json.dumps(myCustomDct, sort_keys=True, indent=4) FlNm.write(tmpJsn) FlNm.close() def bk_loads_json(flNm): "Takes file of the json and returns it as a dict." json_data=open(flNm) data = json.load(json_data) json_data.close() return data