python代码从命令行解析时间参数以打印特定的crontab文件行

发布于 2025-02-01 05:11:20 字数 1634 浏览 3 评论 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")

所示是输出的样子 [sagar] $ ./crontab_summary.py -t 0:0:1 crontab.txt trackback(最近的呼叫最后一个呼叫):文件“/home/crontab/./crontab_summary.py.py”,第95行,在main()

我会喜欢从命令行中获取值0:0:1,将其与crontab.txt文件行进行比较,看看它们是否是匹配还是高于命令行提供的时间。如果是,则将该线打印为输出。如果没有,请打印错误消息(指定时间没有命令行)。

如何编写此代码或修复现有代码?

I'm writing a Python program that takes the time argument from the command line, compares it with the crontab.txt file and if the time value is same or after the time specified in the command line, It prints out the line of crontab.txt what meets the criteria.

crontab file content

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

And python code built looks like below

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")

Here is how output looks like
[Sagar]$ ./crontab_summary.py -t 0:0:1 crontab.txt Traceback (most recent call last): File "/home/Crontab/./crontab_summary.py", line 95, in main()

I would like to take values 0:0:1 from the command line, compare those with the crontab.txt file lines and see if their is any line that either matches or is higher than the time provided in command line. If yes, print that line as the output. If not, print the error message (no command lines for the specified time).

How to write this code or fix the existing code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文