通用网址查询网站,谷歌风格wordpress,网站网站建设考虑要素,用ssh做网站实例代码 # 1、接收用户输入的文件名#xff08;要备份的文件名#xff09;
oldname input(请输入要备份的文件名称#xff1a;) # python.txt
# 2、规划备份文件名#xff08;python[备份].txt#xff09;
# 搜索点号
index oldname.rfind(.)
# 返回文件名和文件后缀
…实例代码 # 1、接收用户输入的文件名要备份的文件名
oldname input(请输入要备份的文件名称) # python.txt
# 2、规划备份文件名python[备份].txt
# 搜索点号
index oldname.rfind(.)
# 返回文件名和文件后缀
name oldname[:index]
postfix oldname[index:]
newname name [备份] postfix
# 3、对文件进行备份操作
old_f open(oldname, rb)
new_f open(newname, wb)# 读取源文件内容写入新文件
while True:content old_f.read(1024)if len(content) 0:breaknew_f.write(content)
# 4、关闭文件
old_f.close()
new_f.close() 补充
遗留问题我们要备份的文件名称都是由用户通过input方法输入而来的但是一定要记住只要在程序中有人为输入强烈建议对用户输入的数据进行校检。
所有用户的输入都是不靠谱的
解决用户输入文件名称异常问题 # 1、接收用户输入的文件名要备份的文件名
oldname input(请输入要备份的文件名称) # python.txt
# 2、规划备份文件名python[备份].txt
# 搜索点号
index oldname.rfind(.)
# 对index进行判断判断是否合理index 0)
if index 0:# 返回文件名和文件后缀name oldname[:index]postfix oldname[index:]newname name [备份] postfix# 3、对文件进行备份操作old_f open(oldname, rb)new_f open(newname, wb)# 读取源文件内容写入新文件while True:content old_f.read(1024)if len(content) 0:breaknew_f.write(content)# 4、关闭文件old_f.close()new_f.close()
else:print(请输入正确的文件名称否则无法进行备份操作...)