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

在Python中反复使用计数器创建文件,第1张

概述基本上我想要做的是编写一个 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", ' 基本上我想要做的是编写一个 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+')    print(i)    time.sleep(1)

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

解决方法
import shutil,itertoolsimport timedirpath = 'c:\usr\'for i in itertools.count():    file = open(dirpath+"file " + str(i) + ".txt",'w+')    print(i)    time.sleep(1)

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

总结

以上是内存溢出为你收集整理的在Python中反复使用计数器创建文件全部内容,希望文章能够帮你解决在Python中反复使用计数器创建文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1193798.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存