[英]How to pass timestamp as a command line argument in python?
I have a crontab which runs a python script.我有一个运行 python 脚本的 crontab。 The python script takes in timestamp as the command line argument.
python 脚本将时间戳作为命令行参数。 But I am unable to figure out how can I get the timestamp.
但我无法弄清楚如何获得时间戳。
I tried this but this is not working.我试过这个,但这不起作用。
2 * * * * python3 test.py \%Y-\%m-\%H\ \%k:\%M:\%S
You can pass it as any other command line argument and parse it something like您可以将它作为任何其他命令行参数传递并解析它
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)
#YEAR
print(args.xDate[0])
$ python test.py 2018-07-14
Output:输出:
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=195, tm_isdst=-1)
2018
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.