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.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
推荐阅读
- java - Java - TreeSet 接受重复项
- ruby-on-rails - 验证 Rails 属性存在但允许空字符串
- javascript - 在 MongoDB 上创建并保存模型的记录
- c# - AutoMapper 用 null 覆盖嵌套字段
- asp.net-core - 从 .NET 核心中的控制器方法更新 appSettings.json
- c++ - 从 NS3 获取简单的指标,例如吞吐量与时间、丢包率与时间
- sql - 重新运行已编辑的 T-SQL 脚本时,必须强制 MS SQL Server (2012) 返回其“原始”状态
- r - 如何用 R 中的预处理值制作一个 3x3 矩阵?
- javascript - 单击时切换按钮的颜色(并在不取消选择页面上所有按钮的情况下恢复其他按钮的颜色)
- asp.net - 错误 415 不支持的媒体类型