python 遍历所有文件,修改文件内的内容
2015-04-16 23:53
411 查看
__author__ = 'Administrator' import os import shutil class CFileOperator(object): def __init__(self): self._m_FilePath = os.getcwd() self._m_FileContent = [] self._m_sError = "" def GetFileContent(self, filepath="", filecontent=[]): if not self.IsFileExit(filepath): self._m_sError = "File Path is not exit %s" % filepath return False openFile = open(filepath, 'r') try: for line in openFile: filecontent.append(line) finally: openFile.close() return True def WriteFileContet(self, filepath="", filecontent=[], isAdd=True): if not self.IsFileExit(filepath): dirpath = filepath[0:filepath.rfind("/")] if not self.ISDirExit(dirpath): self.CreateDir(dirpath) if (True == isAdd): openfile = open(filepath, "a") else: openfile = open(filepath, 'w') try: openfile.writelines(filecontent) finally: openfile.close() def ListFile(self, filepath="", result=[]): FileName = [] self.GetCurrentDirAndFilePath(filepath, FileName) for file in FileName: if file == "." or file == "..": continue else: newfile = filepath + "/" + file if self.ISDirExit(newfile): self.ListFile(newfile, result) else: result.append(newfile) return result def GetCurrentDirAndFilePath(self, path="", content=[]): if not self.ISDirExit(path): self._m_sError = "the file dir is not exit %s" % path return False content.extend(os.listdir(path)) return True def GetCurrentFilePath(self, path="", content=[]): if not self.ISDirExit(path): self._m_sError = "the file dir is not exit %s" % path return False DirFilecontent = os.listdir(path) for elem in DirFilecontent: if self.IsFileExit(path + "/" + elem): content.append(elem) return True def CreateDir(self, filepaht): os.makedirs(filepaht) def RmDir(self, filepath): if self.ISDirExit(filepath): shutil.rmtree(filepath) def IsFileExit(self, filepath): return os.path.isfile(filepath) def ISDirExit(self, DirPath): return os.path.isdir(DirPath) def TarFile(self, filepath): os.chdir(filepath[0:filepath.rfind("/")]) command = "tar -cvf ." + filepath[filepath.rfind("/"):len(filepath)] + \ ".tar" + " ." + filepath[filepath.rfind("/"):len(filepath)] os.chdir(self._m_FilePath) print command os.system(command) def UNtarFile(self, filepath): command = "tar -xvf " + filepath print command os.system(command) def GetError(self): return self._m_sError def modefycpp(elem): cCFileOperator = CFileOperator() content = [] cCFileOperator.GetFileContent(elem, content) for index in range(len(content)): if str(content[index]).find("CCAssert") != -1: line = "//" + str(content[index]) content[index] = line cCFileOperator.WriteFileContet(elem, content, False) if __name__ == "__main__": cCFileOperator = CFileOperator() result = [] cCFileOperator.ListFile("D:\cocos2dx\cocos2d-x-2.2.1", result) cppfile = [] for elem in result: if str(elem).endswith(".cpp"): modefycpp(elem) # else: # print elem
相关文章推荐
- python 遍历指定文件夹下的所有文件内容
- Python脚本- 递归遍历文件夹,获取指定扩展名文件,修改文件内容
- python写一段脚本代码自动完成输入(目录下的所有)文件的数据替换(修改数据和替换数据都是输入的)
- 【Python】遍历文件夹中所有文件
- python遍历文件及其替换文件内容
- python写一段脚本代码自动完成输入(目录下的所有)文件的数据替换(修改数据和替换数据都是输入的)【转】
- python修改文件内容,不需要read,write多个动作。
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- Python 读取excle文件所有内容[xlrd模块]
- Python修改文件内容
- python2.7读取文件夹下所有文件名称及内容的方法
- python文件操作(遍历所有文件)
- linux下批量修改目录下所有文件夹、文件名称及文件内容
- Python批量修改文件夹内所有json文件中部分内容
- Python实现遍历目录下所有文件并写到文件中
- Python实现遍历目录下所有文件并写到文件中
- python 遍历文件夹修改文件后缀
- python基础---实现文件传参,修改文件内的内容
- 批量修改cvs下面所有Root文件的python脚本
- python程序设计基础5:python文件使用(遍历一个文件夹中所有的图片)