python 脚本
(1)在linux4 上编写/root/createfile.py 的 python3 脚 本,创建20 个 文 件 /root/python/file00 至 /root/python/file19,如果文件存在,则删除再创建;每个文 件的内容同文件名,如file00文件的内容为“file00”。
import os
# 创建目标目录
directory = "/root/python"
if not os.path.exists(directory):
os.makedirs(directory)
# 创建文件
for i in range(20):
filename = f"file{i:02d}"
filepath = os.path.join(directory, filename)
# 如果文件存在,则删除
if os.path.exists(filepath):
os.remove(filepath)
# 创建文件并写入内容
with open(filepath, "w") as file:
file.write(filename)