Restore a Deleted File using Python
In Python, restoring a deleted file is possible under certain conditions. If the file was deleted but still exists in the Recycle Bin (Windows) or Trash (Mac/Linux), it can be recovered. However, if it was permanently deleted, recovery depends on specialized recovery tools. In this tutorial, we explore different methods to restore deleted files using Python.
Examples to Restore a Deleted File
1. Restoring a File from Recycle Bin (Windows) Using winshell
In this example, we use the winshell
module to list and restore deleted files from the Recycle Bin in Windows.
main.py
import winshell
# Restore all files from Recycle Bin
for file in winshell.recycle_bin():
print("Restoring:", file.original_filename())
file.restore()
Explanation:
- We import the
winshell
module, which allows interaction with Windows shell functionalities. - The
winshell.recycle_bin()
function retrieves all files in the Recycle Bin. - We iterate over each file and print its original filename using
file.original_filename()
. - Each file is restored using
file.restore()
.
Output:
Restoring: C:\Users\User\Desktop\deleted_file.txt
Restoring: C:\Users\User\Documents\example.docx
This method only works for files that are in the Windows Recycle Bin.
2. Restoring a File from Trash (Linux/Mac) Using send2trash
For Linux and macOS, the send2trash
module moves files to the trash instead of deleting them permanently. We can list and restore files using this module.
main.py
import os
from send2trash import send2trash
# Move file to Trash
file_path = "example.txt"
send2trash(file_path)
print(f"Moved {file_path} to Trash.")
Explanation:
- We import the
send2trash
module, which allows safe file deletion by moving files to Trash instead of permanently deleting them. - The
send2trash(file_path)
function moves the file to the system’s Trash. - A confirmation message is printed.
Output:
Moved example.txt to Trash.
To manually restore the file, go to the Trash folder and move it back.
3. Restoring a Recently Deleted File Using File Backups
If you maintain backups, you can programmatically restore a file by copying it from a backup location.
main.py
import shutil
# Define backup and restore paths
backup_path = "backup/example_backup.txt"
restore_path = "example.txt"
# Restore file from backup
shutil.copy(backup_path, restore_path)
print(f"Restored {restore_path} from backup.")
Explanation:
- We import the
shutil
module for file operations. - Define
backup_path
as the location of the backup file. - Define
restore_path
as the destination for restoring the file. - Use
shutil.copy()
to restore the file from the backup.
Output:
Restored example.txt from backup.
This method works if you have a backup system in place.
4. Attempting Recovery of a Permanently Deleted File Using Data Recovery Tools
If a file is permanently deleted, recovery tools like TestDisk or Photorec can help. Python can automate these tools using the subprocess
module.
main.py
import subprocess
# Run a file recovery command (Example using TestDisk)
command = "photorec /d recovered_files/ /cmd example.txt,search"
subprocess.run(command, shell=True)
Explanation:
- We import the
subprocess
module to run system commands. - The
photorec
command is executed to search for and restore deleted files. - Recovered files are saved in the
recovered_files
directory.
Output:
Recovered files saved in recovered_files/ directory.
This method requires external recovery software.
Conclusion
Python provides different ways to restore deleted files depending on their deletion state:
- Recover from Recycle Bin using
winshell
(Windows). - Recover from Trash using
send2trash
(Linux/Mac). - Restore from a Backup using
shutil.copy()
. - Use Data Recovery Tools like TestDisk for permanently deleted files.