解决csdn自动跳转主页问题
import os
directory = r'D:\project\graduation'
for filename in os.listdir(directory):
if filename.endswith(".html"):
filepath = os.path.join(directory, filename)
with open(filepath, 'r', encoding='utf-8') as file:
content = file.read()
content = content.replace('onerror', 'xxxx')
with open(filepath, 'w', encoding='utf-8') as file:
file.write(content)
print(f'文件 {filename} 已修改。')
多个
import os
# 设置目录路径列表
directories = [
r'D:\project\graduation\20240521全包\xxx',
# 添加其他目录路径...
]
# 遍历每个目录
for directory in directories:
# 遍历目录中的所有文件
for filename in os.listdir(directory):
if filename.endswith(".html"): # 确保处理HTML文件
filepath = os.path.join(directory, filename)
with open(filepath, 'r', encoding='utf-8') as file:
content = file.read()
# 替换onerror为xxxx
content = content.replace('onerror', 'xxxx')
# 写回文件
with open(filepath, 'w', encoding='utf-8') as file:
file.write(content)
print(f'文件 {filename} 已修改。')