我想在python中循环我的文件创建者若干次

我想在python中循环我的文件创建者若干次,python,Python,下面是代码,我不确定我要做什么才能循环它一定的次数 #import stuff import os import random import string #generate ran str letters = string.ascii_lowercase strgen = ( ''.join(random.choice(letters) for i in range(10)) ) #creating file and saving into dir filepath = os.path.jo

下面是代码,我不确定我要做什么才能循环它一定的次数

#import stuff
 import os
import random
import string
#generate ran str
letters = string.ascii_lowercase
strgen = ( ''.join(random.choice(letters) for i in range(10)) )
#creating file and saving into dir
filepath = os.path.join('c:/files/' + strgen + '.txt')
if not os.path.exists('c:/files'):
    os.makedirs('c:/files')
f = open(filepath, "w+")

我不知道我是不是说对了,但你是说下面这样的话吗

count = 0

while count < 10:
     letters = string.ascii_lowercase
     strgen = ( ''.join(random.choice(letters) for i in range(10)) )
     #creating file and saving into dir
     filepath = os.path.join('c:/files/' + strgen + '.txt')
     if not os.path.exists('c:/files'):
     os.makedirs('c:/files')
     f = open(filepath, "w+")         

     count = count + 1
count=0
当计数小于10时:
字母=字符串。ascii_小写
strgen=(“”.join(范围(10)内i的随机.choice(字母))
#创建文件并保存到目录
filepath=os.path.join('c:/files/'+strgen+'.txt'))
如果不存在os.path.exists('c:/files'):
os.makedirs('c:/files')
f=打开(文件路径,“w+”)
计数=计数+1

您想要执行的任何操作都可以放置在操作函数中

在您希望执行此函数的特定时间后开始操作

停止执行的停止间隔

执行次数=停止间隔/设置间隔

导入时间,线程

StartTime=time.time()

def action() :
    print('action ! -> time : {:.1f}s'.format(time.time()-StartTime))


class setInterval :
    def __init__(self,interval,action) :
        self.interval=interval
        self.action=action
        self.stopEvent=threading.Event()
        thread=threading.Thread(target=self.__setInterval)
        thread.start()

    def __setInterval(self) :
        nextTime=time.time()+self.interval
        while not self.stopEvent.wait(nextTime-time.time()) :
            nextTime+=self.interval
            self.action()

    def cancel(self) :
        self.stopEvent.set()

# start action every 1s
inter=setInterval(0.6,action)
print('just after setInterval -> time : {:.1f}s'.format(time.time()-StartTime))

# will stop interval in 5s
t=threading.Timer(5,inter.cancel)
t.start()

你想执行相同的代码,当然不想,这正是我想做的