-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_robust.py
More file actions
26 lines (23 loc) · 950 Bytes
/
replace_robust.py
File metadata and controls
26 lines (23 loc) · 950 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
import sys
try:
with open('studyverse (1).html', 'r', encoding='utf-8', errors='ignore') as f:
lines = f.readlines()
replaced = False
for i, line in enumerate(lines):
if "window.location.href='/resources'" in line and "Resources</button>" in line:
print("Found target line at", i)
# Indentation
indent = len(line) - len(line.lstrip())
space = line[:indent]
physics_button = space + '<button class="btn btn-secondary" onclick="window.location.href=\'/physics\'">Physics Room</button>\n'
lines.insert(i + 1, physics_button)
replaced = True
break
if replaced:
with open('studyverse (1).html', 'w', encoding='utf-8') as f:
f.writelines(lines)
print("Successfully replaced and saved!")
else:
print("Target line not found.")
except Exception as e:
print(f"Error: {e}")