每15分钟更改一次日志文件名称,而不会覆盖

时间:2018-08-19 17:11:12

标签: python python-3.x datetime

我需要将程序输出写入一个日志文件,该文件的名称应每15分钟更改一次, 目前我正在这样做。

import time
from datetime import datetime

FullLog = datetime.today().strftime("Prog1TempLog1_%d_%m_%Y") + ".txt"

today0915 = datetime.now().replace(hour=9, minute=15, second=0,microsecond=0)
today0930 = datetime.now().replace(hour=9, minute=30, second=0,microsecond=0)
today0945 = datetime.now().replace(hour=9, minute=45, second=0,microsecond=0)
today1000 = datetime.now().replace(hour=10, minute=0, second=0,microsecond=0)
today1015 = datetime.now().replace(hour=10, minute=15, second=0,microsecond=0)

while <my condition>:

    if datetime.now() > today0915 and datetime.now() <= today0930:
        FullLog = datetime.today().strftime("Prog1Log1_%d_%m_%Y") + ".txt"
    if datetime.now() > today0930 and datetime.now() <= today0945:
        FullLog = datetime.today().strftime("Prog1Log2_%d_%m_%Y") + ".txt"
    if datetime.now() > today0945 and datetime.now() <= today1000:
        FullLog = datetime.today().strftime("Prog1Log3_%d_%m_%Y") + ".txt"
    if datetime.now() > today0945 and datetime.now() <= today1000:
        FullLog = datetime.today().strftime("Prog1Log4_%d_%m_%Y") + ".txt"
    if datetime.now() > today1000 and datetime.now() <= today1015:
        FullLog = datetime.today().strftime("Prog1Log5_%d_%m_%Y") + ".txt"

    print("Time", datetime.now(), "FileName:", FullLog)

    time.sleep(1)

是否有任何动态方法可以执行此过程。

1 个答案:

答案 0 :(得分:2)

在while循环期间,获取当前时间。然后找出它有多少分钟。例如,10:20有20分钟。现在,将其除以15,然后加1以获取文件名。

print (df)

   Group  Value1  Value2  TargetColumn
0      1       1       2             0
1      1       2       2             1
2      1       3       3             1
3      1       4       4             1
4      2       6       9             0
5      2       7       5             0
6      2       8       6             0
7      2       9       7             0