-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfind_and_replace.py
More file actions
32 lines (27 loc) · 1.53 KB
/
find_and_replace.py
File metadata and controls
32 lines (27 loc) · 1.53 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
# Useful find-and-replace script for replacing text in multiple files
# originally from https://umatechnology.org/find-and-replace-text-in-multiple-files-in-bulk-on-windows-11-10/
import os
# Function to search and replace text in files
def find_and_replace(dir_path, file_extension, find_text, replace_text):
replace_count = 0
for foldername, subfolders, filenames in os.walk(dir_path):
print(f'Found {len(filenames)} files...')
for filename in filenames:
if filename.endswith(file_extension): # You can specify other files types
file_path = os.path.join(foldername, filename)
try:
with open(file_path, 'r', encoding='utf-8') as file:
file_contents = file.read()
replace_count += file_contents.count(find_text)
new_contents = file_contents.replace(find_text, replace_text)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_contents)
except Exception as e:
print(f'[ERROR] Skipped ({file_path}) because of exception:\n{e}\nMoving on...')
print(f'Complete!\nReplaced {replace_count} occurances!')
# Usage
dir_path = '/home/superuser/Documents/GitHub/perspector.github.io'
file_extension = ".html"
find_text = '<p>Copyright © 2023 - 2025 by Benjamin Chase</p>'
replace_text = '<p>Copyright © 2023 - 2026 by Benjamin Chase</p>'
find_and_replace(dir_path, file_extension, find_text, replace_text)