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

Python 每15分钟更改一次日志文件名,不覆盖,python,python-3.x,datetime,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 =

我需要将程序输出写入一个日志文件,该文件的名称应每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)
导入时间
从日期时间导入日期时间
FullLog=datetime.today().strftime(“Prog1TempLog1_%d_%m_%Y”)+“.txt”
today0915=datetime.now().replace(小时=9,分钟=15,秒=0,微秒=0)
today0930=datetime.now().replace(小时=9,分钟=30,秒=0,微秒=0)
today0945=datetime.now().replace(小时=9,分钟=45,秒=0,微秒=0)
today1000=datetime.now().replace(小时=10,分钟=0,秒=0,微秒=0)
today1015=datetime.now().replace(小时=10,分钟=15,秒=0,微秒=0)
而:

如果datetime.now()>today0915和datetime.now()today0930和datetime.now()today0945和datetime.now()today0945和datetime.now()today1000和datetime.now()在while循环期间,获取当前时间。然后算出它有多少分钟。例如,10:20有20分钟。现在将其除以15,再加1以获得文件名

now = datetime.today().minute
log_seq = num//15 + 1 #just check edge cases around 60 and 00 .. // for python3
FullLog = datetime.today().strftime("Prog1Log"+log_seq+"_%d_%m_%Y") + ".txt"

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

now = datetime.today().minute
log_seq = num//15 + 1 #just check edge cases around 60 and 00 .. // for python3
FullLog = datetime.today().strftime("Prog1Log"+log_seq+"_%d_%m_%Y") + ".txt"

请注意,问题是关于Python3的,因此
num/15
将产生一个浮点。非常感谢。我确实注意到python3的事情,但没想到p3中修改了数字运算符等基本函数。但是如何处理60和00左右的边缘情况呢?。日志文件名的连续编号也适用于我。这是您需要弄清楚的。您希望00是日志文件4还是1。类似的事情可以通过if/elseI检查。我希望日志序号像logfile4一样继续。请注意,问题是关于Python 3的,因此
num/15
将产生一个浮点。非常感谢。我确实注意到python3的事情,但没想到p3中修改了数字运算符等基本函数。但是如何处理60和00左右的边缘情况呢?。日志文件名的连续编号也适用于我。这是您需要弄清楚的。您希望00是日志文件4还是1。类似的事情可以通过if/elseI检查。我希望日志序号像logfile4一样继续。也许你应该使用
logging
包和。也许你应该使用
logging
包和。