-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplace.py
More file actions
51 lines (43 loc) · 1.5 KB
/
replace.py
File metadata and controls
51 lines (43 loc) · 1.5 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
repo_path = "/home/paperalt/Documents/paperalt.github.io"
replacements = {
"paperalt": "paperalt",
"Paperalt": "Paperalt",
"PAPERALT_ID": "PAPERALT_ID",
"PAPERALT": "PAPERALT",
}
def replace_in_file(filepath):
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
except Exception as e:
print(f"Error reading {filepath}: {e}")
return False
new_content = content
for old, new in replacements.items():
new_content = new_content.replace(old, new)
if new_content != content:
try:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
except Exception as e:
print(f"Error writing {filepath}: {e}")
return False
return False
def main():
modified_files = []
print(f"Walking {repo_path}")
for root, dirs, files in os.walk(repo_path):
# Prevent os.walk from entering specific directories
dirs[:] = [d for d in dirs if d not in ['.git', 'img', 'font', 'fonts']]
for file in files:
if not file.endswith(('.html', '.xml', '.json', '.py', '.txt', '.md', '.css', '.js')):
continue
filepath = os.path.join(root, file)
if replace_in_file(filepath):
modified_files.append(filepath)
for f in modified_files:
print(f"Updated {f}")
if __name__ == "__main__":
main()