网页保存后,会把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('.'))


相关文章:

  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
猜你喜欢
  • 2022-12-23
  • 2021-09-16
  • 2022-02-09
  • 2022-02-09
  • 2021-12-21
  • 2022-01-07
相关资源
相似解决方案