首页 > 解决方案 > 我想在 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.join('c:/files/' + strgen + '.txt')
if not os.path.exists('c:/files'):
    os.makedirs('c:/files')
f = open(filepath, "w+")

标签: python

解决方案


不知道我说得对不对,但你的意思是像下面这样吗?

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

推荐阅读