用Python代码解析来自命令行的时间参数,以打印crontab文件的特定行。

0 人关注

我在写一个Python程序,它从命令行获取时间参数,与crontab.txt文件进行比较,如果时间值与命令行中指定的时间相同或在其之后,它将打印出符合标准的crontab.txt行。

crontab文件内容

50,11,3,Sagar,/home/Sagar/backup 
30,17,4,sahara,/home/sahara/data/backup
0,0,6,root,/usr/bin/find / -name a.out

构建的python代码看起来如下

elif(args.time):
    if contents ==[]:
        print('No crontab lines for time specified')
    else:
        flag=False
        for line in contents:
            second_col = line.split(',')[2]
            if( args.time == second_col.split(':')[-1]):
                if(flag==False):
                    print('Output:')
                print(line)
                flag = True
        if(not flag):
            print('No crontab lines for time specified')
elif(args.time):
    if contents == [] :
        print("No command lines for the specified time")
    else:
        for line in contents:
            t1 = line.split(' ')[0].split(':')[0]
            if args.time == 'M' and int(t1) == datetime.datetime.now:
                print(line)
            elif args.time == 'H' and int(t1) == datetime.datetime.now:
                print(line)
            elif args.time == 'D' and int(t1) == datetime.datetime.now:
                    print(line)
            else:
                print("No command lines for the specified time")