-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvert.py
94 lines (74 loc) · 2.71 KB
/
Convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
path = r"C:\AMAT\AA"
replaceStr = {'<iostream.h>':'<iostream>', '<aaa.h>': '<aaa>'}
def getFlist():
L = []
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
#print(name)
flag = name.endswith(".cpp") or name.endswith(".h")
if not flag :
continue
L.append(os.path.join(root, name))
return L
# 传入文件(file),将旧内容(old_content)替换为新内容(new_content)
def replace(file):
content = read_file(file)
for key in replaceStr:
print(key)
old_content = key
new_content = replaceStr[key]
content = content.replace(old_content, new_content)
rewrite_file(file, content)
# 读文件内容
def read_file(file):
with open(file, encoding='UTF-8') as f:
read_all = f.read()
f.close()
return read_all
# 写内容到文件
def rewrite_file(file, data):
with open(file, 'w', encoding='UTF-8') as f:
f.write(data)
f.close()
# 替换操作(将test.txt文件中的'Hello World!'替换为'Hello Qt!')
def modifyFile(srcflist):
print('----')
# 修改.h 文件
for i in srcflist:
if i.endswith(".h") :
print('modify .h filename :' + i)
replace(i)
def modifyInsertArgs(srcflist):
for i in srcflist:
if i.endswith(".h"):
with open(i, "r", encoding="utf-8") as f:
lines = f.readlines()
with open(i, "w", encoding="utf-8") as f_w:
for line in lines:
if not '.insertArgs("%s' in line:
continue
line = line.replace(line, "ID_NUM = 22")
# strlist = line.split(',') # 用逗号分割str字符串,并保存到列表
# for value in strlist:
f_w.write(line)
def processFile(srcflist) :
for i in srcflist:
# if i.endswith(".h"):
with open(i, "r", encoding="utf-8") as f:
lines = f.readlines()
with open(i, "w", encoding="utf-8") as f_w:
for line in lines:
# if not '.insertArgs("%s' in line:
# continue
# if line is ema
line=line.strip('\n')
a= '<' + line + '>' + '</' + line + '>'+'\n'
line = line.replace(line, '<' + line + '>' + '</' + line + '>'+'\n')
# strlist = line.split(',') # 用逗号分割str字符串,并保存到列表
# for value in strlist:
f_w.write(line)
if __name__ == "__main__" :
srcflist = getFlist()
print(srcflist)
processFile(srcflist)