dt 为设定的时间,转化为时间戳,在主循环里一分钟循环一次,读取当前的时间戳,当前时间大于设定时间,清空目录。
import os,sysimport shutilimport timedef clear(): cur_file = os.path.basename(sys.argv[0]) dir_content = [x for x in os.listdir(".") if x != cur_file] for f in dir_content: if os.path.isdir(f): shutil.rmtree(f) else: os.remove(f)if __name__ == "__main__": dt = "2017-10-25 10:58:54" timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S") timestamp = time.mktime(timeArray)# print(timestamp) while True: now_time = time.time() time.sleep(60) if(now_time>timestamp): clear() exit()
1
import os,sys
2
import shutil
3
import time
4
def clear():
5
cur_file = os.path.basename(sys.argv[0])
6
dir_content = [x for x in os.listdir(".") if x != cur_file]
7
for f in dir_content:
8
if os.path.isdir(f):
9
shutil.rmtree(f)
10
else:
11
os.remove(f)
12
13
if __name__ == "__main__":
14
dt = "2017-10-25 10:58:54"
15
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
16
timestamp = time.mktime(timeArray)
17
# print(timestamp)
18
while True:
19
now_time = time.time()
20
time.sleep(60)
21
if(now_time>timestamp):
22
clear()
23
exit()