-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path44-add-utf8.py
More file actions
42 lines (33 loc) · 1.32 KB
/
44-add-utf8.py
File metadata and controls
42 lines (33 loc) · 1.32 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
import os
import sys
import re
def add_charset_to_html_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Check if <meta charset="UTF-8"> already exists
if re.search(r'<meta\s+charset\s*=\s*["\']?UTF-8["\']?\s*/?>', content):
print(f"Charset already exists in {file_path}. Skipping.")
return
# Add <meta charset="UTF-8"> under the <head> tag
content = re.sub(r'(<head[^>]*>)', r'\1\n <meta charset="UTF-8">', content, flags=re.IGNORECASE)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Charset added to {file_path}")
except Exception as e:
print(f"Error processing {file_path}: {e}")
def process_directory(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.lower().endswith('.html'):
file_path = os.path.join(root, file)
add_charset_to_html_file(file_path)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <directory>")
sys.exit(1)
directory = sys.argv[1]
if not os.path.isdir(directory):
print(f"Error: {directory} is not a valid directory.")
sys.exit(1)
process_directory(directory)