随机设置时间
import random
day = str(random.randint(1,10)).zfill(2)
time = str(random.randint(8,18)).zfill(2)
minute =str(random.randint(0,59)).zfill(2)
second =str(random.randint(0,59)).zfill(2)
time1= f'2022:08:{day} {time}:{minute}:{second}'
print(time1)
时间转换
import time
imgTime = "2022:08:16 17:47:55"
timeArray = time.strptime(imgTime, "%Y:%m:%d %H:%M:%S")
timeStamp = int(time.mktime(timeArray))
print(timeStamp)
设置文件修改时间和访问时间
import os
os.utime(photoPwd,(timeStamp,timeStamp)) #photoPwd为文件完整路径