替换工程文件中特殊字符,比如某类注释或者空行的空格,用脚本就比较方便了。
例如写一个config.ini的配置文件:
[dir]
dir1 = D:/test1
dir2 = D:/test2
然后就是用configparser库去读取配置文件,re正则表达式库去匹配对应的字符串,代码如下:
import sys
import os
import re
import configparser
import commonFunc
def lineRelace(content, path):
curPos = 0
ret = False
inComment = False
while True:
match = re.search(r'\s*/\* remove comment*?\*/[ \t]*', content[curPos:], re.S)
if match is None:
break
express = match.group()
content = content[:curPos + match.start()] + content[curPos + match.end():]
curPos += match.end()
print('%s: %d %s'%(path, content[:curPos].count('\n') + 1, express))
ret = True
return content
def find_and_replace_check(filePath, encoding):
if re.search(r'fdd', filePath):
return
with open(filePath, 'r', newline='', errors='ignore', encoding = encoding) as file:
content = file.read()
fileContent = lineRelace(content, filePath)
with open(filePath, "w", newline='', errors='ignore', encoding=encoding) as replaceFile:
replaceFile.write(fileContent)
def doSomeProc(path):
if not os.path.exists(path):
print('please input right path(%s)'%(path))
return 1
fileQueue = commonFunc.get_file_queue(path)
for filePath in fileQueue:
encoding = commonFunc.get_file_encoding(filePath)
find_and_replace_check(filePath, encoding)
return
def main():
items = commonFunc.read_cfg_dir('config-items.ini')
for item in items:
dir_path = item[1]
print("dir_path:%s" % dir_path)
doSomeProc(dir_path)
if __name__ == "__main__":
main()
input('\n\nplease press Enter key to exit!')
---------------
import configparser
import os
SUBFIX = ['.c', '.cpp', '.h', '.inc']
# 说明:UTF兼容ISO8859-1和ASCII,GB18030兼容GBK,GBK兼容GB2312,GB2312兼容ASCII
CODES = ['UTF-8','GB18030']
UTF_8_BOM = b'\xef\xbb\xbf'
def get_file_queue(path):
fileQueue = []
if os.path.isfile(path):
fileQueue.append(path)
else:
SearchDir(path, fileQueue)
return fileQueue
def SearchDir(dir, fileQueue):
for f in os.listdir(dir):
path = os.path.join(dir, f)
if os.path.isfile(path):
tmp = os.path.splitext(f)
if tmp[-1] in SUBFIX:
fileQueue.append(path)
elif os.path.isdir(path):
SearchDir(path, fileQueue)
return
def get_file_encoding(file_path):
with open(file_path, 'rb') as f:
return string_encoding(f.read())
def string_encoding(b):
for code in CODES:
try:
b.decode(encoding=code)
if 'UTF-8' == code and b.startswith(UTF_8_BOM):
return 'UTF-8-SIG'
return code
except Exception:
continue
return 'gbk'
def read_cfg_dir(fileName):
cf = configparser.ConfigParser()
cf.read(fileName)
items = cf.items("cfg")
return items
def read_cfg_file(fileName):
cf = configparser.ConfigParser()
cf.read(fileName)
url_path = cf.get("cfg", "dir")
print(url_path)
return url_path