需求:输入文件的名字,然后程序自动完成对文件进行备份
完整参考代码
oldFileName = input("请输入要拷贝的文件名字:")
oldFile = open(oldFileName,"r")
#如果打开文件
if oldFile:
#提取文件的后缀
fileFlagNum = oldFileName.rfind(".")
if fileFlagNum >0:
#创建新文件
newFile = open(newFileName,"w")
#把旧文件中的数据,一行一行的进行复制到新文件中
for lineContent in oldFile.readlines():
newFile.write(lineCotent)
#关闭文件
oldFile.close()
newFile.close()
分步分析,逐步实现
image.png image.png image.png
网友评论