for serial_num, line in enumerate(my_lines):
serial_num = serial_num + 1
if re.match(r"\s*#+.*", line): # 单行注释,1个井号
serial_num_list[0].append(serial_num)
for ser in serial_num_list[0]:
serial_num_sum1 += 1
try:
for ser_num, value in enumerate(serial_num_list[1]):
if ser_num % 2 == 0:
top_num = value
else:
end_num = value
serial_num_sum2 += (int(end_num) - int(top_num) + 1)
except:
print("%s源码注释不规范!自动跳过!\n" % dir_path_pure)
continue
if len(willSaveList)>=2:
if willSaveList[len(willSaveList) -1] == '\n' and willSaveList[len(willSaveList) -2].find(codeAnnotationDec)>-1:
willSaveList.pop()
def doDealCode(readFilePath,writeFile):
#如果传递进来的是文件,就直接加入处理文件列表
if os.path.isfile(readFilePath):
fileList.append(dir.decode("utf-8"))
writeFileList.append(dir.decode("utf-8"))
#把读取目录的所有文件加入文件列表
elif os.path.isdir(readFilePath):
for singleFile in os.listdir(readFilePath):
fileList.append(os.path.join(readFilePath,singleFile))
writeFileList.append(os.path.join(writeFile,singleFile))
if 1:
fileIndex = 0
for readSingleFile in fileList:
#处理后的代码行,每行都会插入列表中
willSaveList =[]
fileIndex = fileList.index(readSingleFile)
#初步处理文件行,依次加入一个列表中
with open(readSingleFile,'r') as fileLine:
while True:
#整行读取数据
lines = fileLine.readline()
if not lines:
break
#如果该行不为空
else:
# 该行有 单行注释
if lines.find(codeAnnotationDec)>-1:
#该行为存粹一行注释
if lines.strip().find(codeAnnotationDec) == 0:
willSaveList.append(lines)
#该行既有代码,也有注释
else:
#处理json文件,默认注释和代码放在一行:
if lines.find(":")>-1:
willSaveList.append(lines)
#写入同名文件到另一个目录下
with open(writeFileList[fileIndex],'w') as tempWrite:
print("------ WRITE SUC------",writeFileList[fileIndex]);
print(writeFileList[fileIndex])
for line in willSaveList:
tempWrite.write(line)