首页 > 解决方案 > 如何在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。

标签: pythonpython-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()

推荐阅读