赞
踩
2022 年全国职业院校技能大赛网络搭建与应用赛项正式赛卷 Liunx 脚本
任务描述:请采用脚本,实现快速批量的操作
在 linux5 上编写/root/createfile.py 的 python3 脚本,创建 20 个文件 /root/python/file00至/root/python/file19,如果文件存在,则删除后再创建;每个文件的内容同文件名,如 file00 文件的内容为“file00”。
yum -y install python*
touch createfile.py //创建脚本文件
mkdir /root/python //创建Python目录
vim createfile.py
import os,shutil //导入模块
for a in range(0,20):
b = '%02d' % a //定义变量(命名名字为两位数,不够时补零)
filename = '/root/python/file' + str(b) //定义路径(文件路径+文件名)
if os.path.exists(filename): //检查路径下是否存在相同文件
print(filename + '文件已存在')
os.remove(filename)
print(filename + '文件已删除')
with open(filename,"w") as NR: //创建文件【"w"定义文件写入内容;写入来源NR(模块名称可自取)】
NR.write("file" + str(b))
print(filename + '文件已创建')
python3 createfile.py //执行脚本文件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。