任务描述
输入文件的名字,然后程序自动完成对文件进行备份
应用1:制作文件的备份

应用1:制作文件的备份

参考代码

#提示输入文件
oldFileName = input(“请输入要拷贝的文件名字:”)

#以读的方式打开文件
oldFile = open(oldFileName,‘rb’)

#提取文件的后缀
fileFlagNum = oldFileName.rfind(’.’)
if fileFlagNum > 0:
fileFlag = oldFileName[fileFlagNum:]

#组织新的文件名字
newFileName = oldFileName[:fileFlagNum] + ‘[复件]’ + fileFlag

#创建新文件
newFile = open(newFileName, ‘wb’)

#把旧文件中的数据,一行一行的进行复制到新文件中
for lineContent in oldFile.readlines():
newFile.write(lineContent)

#关闭文件
oldFile.close()
newFile.close()

相关文章:

  • 2021-12-03
  • 2022-12-23
  • 2021-07-21
  • 2022-03-08
  • 2022-12-23
  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
猜你喜欢
  • 2022-01-29
  • 2022-12-23
  • 2022-01-04
  • 2022-02-13
  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
相关资源
相似解决方案