我想在python当前时间的一个小时后创建一个文件夹。我知道如何获取当前时间和日期以及如何创建文件夹。但是如何在我指定的时间创建文件夹。任何帮助将不胜感激。
from datetime import datetime
from datetime import timedelta
import os
while True:
now = datetime.now ()
#print(now.strftime("%H:%M:%S"))
y = datetime.now () + timedelta (hours = 1)
#print(y.strftime("%H:%M:%S"))
if now== y:
os.makedirs (y.strftime ("%H/%M/%S"))
这行得通吗?
编辑:-我必须连续运行代码,即在每个瞬间创建文件夹
答案 0 :(得分:1)
尝试这个简单的代码
import os
import time
while True:
time.sleep(3600) # pending for 1 hour (3600 seconds)
os.makedirs(your directory) # create the directory
编辑(使用并行编程)
import os
import time
from datetime import datetime
from multiprocessing import Pool
def create_folder(now):
# you can manipulate variable "now" as you wish
time.sleep(3600) # pending for 1 hour (3600 seconds)
os.makedirs(your directory) # create the directory
return
while True:
pool = Pool()
now = datetime.now()
result = pool.apply_async(create_folder, [now]) # asynchronously evaluate 'create_folder(now)'
这可能会消耗您的许多计算机资源
答案 1 :(得分:0)
检查此帖子以获得更好的解释,您可以创建一个将在给定时间后运行的函数,并且可以使用此函数通过简单的一行代码来创建文件夹 os.makedirs(“路径\目录名称”) Python - Start a Function at Given Time
答案 2 :(得分:0)
每60秒创建多个文件夹,例如New1,New2,...
import time
while True:
time_Begin = time.time()
print("Creating Folder....")
# CODE FOR CREATING FOLDER AND CONDITION
time_End = time.time()
time_Elapsed = time_End - time_Begin
time.sleep(60-time_Elapsed)
直到外部流程未完成
import time
import random
def creatingFolder():
while externalProcess() != 30:
timeBegin = time.time()
print("Creating Folder....", timeBegin)
timeEnd = time.time()
timeElapsed = timeEnd - timeBegin
time.sleep(5-timeElapsed)
def externalProcess():
return random.randint(1, 30)
creatingFolder()