-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSelf-Healing.py
More file actions
35 lines (27 loc) · 845 Bytes
/
Self-Healing.py
File metadata and controls
35 lines (27 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
main.py
--------
This file runs your target Python code (mainfile.py).
If there is any error, it automatically restores mainfile.py
from a safe backup copy (mainfile.txt).
"""
import time
try:
# Try running the main code file
import mainfile
except Exception as e:
print("----- Error in the code -----")
print("Error details:", e)
time.sleep(1.5)
print("\n----- Recovering the code -----")
# Read backup code from mainfile.txt
with open("mainfile.txt", "r") as backup:
code = backup.read()
# Overwrite mainfile.py with the backup code
with open("mainfile.py", "w") as target:
target.write(code)
time.sleep(1.5)
print("File recovered successfully! Now running recovered file...\n")
time.sleep(1.5)
# Import the recovered file
import mainfile