2022年中职网络搭建国赛创建file题目

78 阅读1分钟

# 定义文件夹路径
folder_path = "./"

#确保文件夹存在
if not os.path.exists(folder_path):
	os.makedirs(folder_path)

#循环创建20个文件
for i in range(20):
	#定义文件名和路径
	file_name = "file" + "{:02d}".format(i)
	file_path = os.path.join(folder_path, file_name)

	#如果文件已经存在,则删除
	if os.path.exists(file_path):
		os.remove(file_path)

	#创建新文件并写入内容
	with open(file_path,"w") as f:
		f.write(file_name)

作个人学习