网页保存后,会把js文件起名为.下载,html里面的引用也会有,很不美观,解决方案:用python替换字符串
import os
import re
"""将当前目录下所有文档进行替换操作"""
def change_str(path):
str_pattern = r"\.下载"
str_new = r""
path_list = os.listdir(path)
for file in path_list:
abs_path = os.path.join(path, file)
if os.path.isfile(abs_path):
if re.search('(\.html)|(\.txt)',file):
print(abs_path)
with open(abs_path, 'r', encoding="utf-8") as f:
str_all = f.read()
with open(abs_path, 'w', encoding="utf-8") as f:
f.write(re.sub(str_pattern, str_new, str_all))
# 修改文件名
if ".下载" in abs_path:
new_name = re.sub("\.下载", '', abs_path)
os.rename(abs_path, new_name)
else:
change_str(abs_path)
change_str(os.path.abspath('.'))
原文地址:https://www.cnblogs.com/birdofparadise/p/9008242.html
时间: 2024-11-07 04:23:52