加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在Python中反复使用计数器创建文件

发布时间:2020-12-20 11:56:11 所属栏目:Python 来源:网络整理
导读:基本上我想要做的是编写一个 python脚本,用于创建文件名中带有count-number的文件,例如“file 1.txt”“file 2.txt”“file 3.txt”. 我走到这一步: import shutil,os,itertoolsfor i in itertools.count(): file = open("FILE " + str(i) + ".txt",'w+') p
基本上我想要做的是编写一个 python脚本,用于创建文件名中带有count-number的文件,例如“file 1.txt”“file 2.txt”“file 3.txt”.

我走到这一步:

import shutil,os,itertools


for i in itertools.count():
    file = open("FILE " + str(i) + ".txt",'w+')
    print(i)
    time.sleep(1)

基本上我能做的就是数数,但文件创建是我的问题. open()似乎不起作用.如何创建这些文件,如何选择directorys来存储文件?

解决方法

import shutil,itertools
import time

dirpath = 'c:usr'
for i in itertools.count():
    file = open(dirpath+"FILE " + str(i) + ".txt",'w+')
    print(i)
    time.sleep(1)

这会奏效.你的代码运行正常.我刚刚添加了目录路径

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读