python - 如何在python 2.7中制作大量文件
问题描述
我的目标是制作选定数量的小型 .txt 文件。我当前的脚本非常基本:
file=open("1234.txt","w")
file.write("This is my file")
file.close()
我想我必须做这样的事情,但我得到无效的语法错误。
name=file.txt()
filename=name+1()
file=open(file,"w")
file.write("this is my file")
它需要制作选定数量的文件,这些文件实际上被称为相同的东西,因为我可以在之后批量编辑它们。
我正在使用 Python 2.7。
解决方案
你可以这样做:
for i in range(0, 10):
file_name = f"file{i}.txt"
file = open(file_name, "w")
file.write("This is my file")
file.close()
推荐阅读
- sql - 在错误子句聚合中出现错误的情况下的变量
- android - 任何人都可以解释 In App Review 弹出窗口的功能
- realm - 默认情况下,mongodb 领域在设备上保留多长时间?
- python - 将从 PostgreSQL 获取的列表的数据类型从 NoneType 更改为 int
- android - 将gradle版本3.6.0升级到最新的4.1.0后出现android view inflate异常(仅在更新应用程序时发生异常)
- javascript - Reactjs:设置变量后如何调用useFetch
- angular - 使用 Angular 从 html 和 css 创建可搜索的 pdf - 不适用于 jspdf
- angular - Angular 组件 UI
- tomcat - 从 Jhipster 3.9.0 中删除码头服务器
- filter - 如何在 fluent-bit 中覆盖 rewrite_tag