python实现的用于搜索文件并进行内容替换的类实例

时间:2021-05-22

本文实例讲述了python实现的用于搜索文件并进行内容替换的类。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/python -O# coding: UTF-8"""-replace string in files (recursive)-display the difference.v0.2 - search_string can be a re.compile() object -> use re.sub for replacingv0.1 - initial version Useable by a small "client" script, e.g.:-------------------------------------------------------------------------------#!/usr/bin/python -O# coding: UTF-8import sys, re#sys.path.insert(0,"/path/to/git/repro/") # Please change pathfrom replace_in_files import SearchAndReplaceSearchAndReplace( search_path = "/to/the/files/", # e.g.: simple string replace: search_string = 'the old string', replace_string = 'the new string', # e.g.: Regular expression replacing (used re.sub) #search_string = re.compile('{% url (.*?) %}'), #replace_string = "{% url '\g<1>' %}", search_only = True, # Display only the difference #search_only = False, # write the new content file_filter=("*.py",), # fnmatch-Filter)-------------------------------------------------------------------------------:copyleft: 2009-2011 by Jens Diemer"""__author__ = "Jens Diemer"__license__ = """GNU General Public License v3 or above - http://pile('{% url (.*?) %}'), #replace_string = "{% url '\g<1>' %}", search_only=True, # Display only the difference# search_only = False, # write the new content file_filter=("*.py",), # fnmatch-Filter )

希望本文所述对大家的Python程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章