Python学习作业三

文件备份

# 1、输入备份文件名
old_name = input('请输入需要备份的文件名:')
# 2、标注备份文件名(运用:rfind函数、切片、判断)
point = old_name.rfind('.')
if point > 0:
    pre_con = old_name[point:]
new_name = old_name[:point]+'[备份]'+pre_con
# 3、写入备份文件内容,先打开文件,并声明访问模式
old_f = open(old_name, 'rb')
new_f = open(new_name, 'wb')
# 3.1、防止文件过大,无法一次读写,执行读写操作
while True:
    con = old_f.read(1024)
    if len(con) == 0:
        break
    new_f.write(con)
# 4、关闭文件
old_f.close()
new_f.close()
print('备份完成!')

存在不足,请多指教。

posted @ 2020-07-17 12:09  终觉浅、要躬行  阅读(161)  评论(0)    收藏  举报