码迷,mamicode.com
首页 > 编程语言 > 详细

python 遍历所有文件,修改文件内的内容

时间:2015-04-17 07:14:46      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:python

__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


本文出自 “风清扬song” 博客,请务必保留此出处http://2309998.blog.51cto.com/2299998/1633691

python 遍历所有文件,修改文件内的内容

标签:python

原文地址:http://2309998.blog.51cto.com/2299998/1633691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!