如何使用Python恢复已删除的文件
使用Python代码恢复已删除的文件,需要先进行文件备份。本文提供示例代码使用shutil模块来恢复备份文件。
import shutil
def restore_file(backup_path, restore_path):
try:
shutil.copy(backup_path, restore_path)
print('文件已成功恢复!')
except FileNotFoundError:
print('备份文件不存在!')
# 指定备份文件路径和恢复路径
b backup_path = 'path/to/backup/file.txt'
restore_path = 'path/to/restore/file.txt'
# 调用恢复文件函数
restore_file(backup_path, restore_path)
该代码中,restore_file函数使用shutil.copy方法将备份文件复制到恢复路径。如果备份文件不存在,则会抛出FileNotFoundError异常。
使用该代码时,需要将backup_path和restore_path变量替换为实际的备份文件路径和恢复路径。
原文地址: https://www.cveoy.top/t/topic/qhQN 著作权归作者所有。请勿转载和采集!