给.h .cpp文件批量在文件末尾加换行

需求是 windows下的文件在linux下编译总是报文件末尾需要换行的警告。

import os
import re
import time

PATH=["src1","src2"]

def Convert(foldpath):
	for root, dirs, files in os.walk(foldpath):
		for f in files:
			filename = root +"/"+ f
			if f.endswith(".h") or f.endswith(".cpp"):
				#print filename
				ConvertFile(filename)

def ConvertFile(filename):
	fp = file(filename, "r")
	data = fp.read()
	fp.close()
	if len(data) <= 0:
		return
	
	if data[-1] != "\n":
		AddLine(filename)

def AddLine(filename):
	fp = file(filename, "a+")
	fp.write("\n")
	fp.close()
	
def main():
	for foldpath in PATH:
		Convert(foldpath)
		
if __name__ == "__main__":
	main()


你可能感兴趣的:(Python)