python 文件處理

__author__ = 'root' 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章