A quick but rough way is to use sys.argv .
import sys xDate = sys.argv[1]
A more robust, extensible way is to use the argparse module:
import argparse parser=argparse.ArgumentParser() parser.add_argument('xDate') args=parser.parse_args()
Then, to access the value provided by the user, use args.xDate instead of xDate .
Using the argparse module, you automatically receive a free help message when a user types
delete_images.py -h
It also gives a useful error message if the user cannot provide the correct entries.
You can also easily set the default value for xDate , convert xDate to a datetime.date object and, as they say on TV, "much, much more!".
Below you see the script you are using
expDate = time.strptime(xDate, '%Y-%m-%d')
to convert the xDate string to a temporary tuple. You can do this with argparse , so args.xDate will be a time tuple automatically. For instance,
import argparse import time def mkdate(datestr): return time.strptime(datestr, '%Y-%m-%d') parser=argparse.ArgumentParser() parser.add_argument('xDate',type=mkdate) args=parser.parse_args() print(args.xDate)
upon startup as follows:
% test.py 2000-1-1
gives
time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1)
PS. Whichever method you choose (sys.argv or argparse), it would be nice to pull
expDate = time.strptime(xDate, '%Y-%m-%d')
out of for-loop . Since the value of xDate never changes, you only need to calculate expDate once.