python文件 *** 作

python文件 *** 作,第1张

概述python文件 *** 作

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#! /usr/bin/python# -*- Coding:utf-8 -*-'''Created on 2013-12-11@author: Java'''import osimport MysqLdbfrom db.DbUtil import DbUtilimport timeimport shutilimport sysimport zipfilefrom os.path import  join,getsizeimport mathclass fileOption():    #global fileList  类的全局变量  放置位置         def __init__(self):        pass        def CreateFloderByList(self,pathList):        '''        创建文件夹        :param pathList:文件夹集合        '''        self.pathList = pathList        for path in pathList:            if not os.path.isdir(path):                os.makedirs(path)                    def CreateFloder(self,path):                self.path = path        if not os.path.isdir(path):            os.makedirs(path)                    def readfilename(self,path):        filenames = os.Listdir(path)        return filenames        def renamefile(self,path):        '''        批量修改文件的名称        :param path:        '''        self.path = path                    allRenameList = os.Listdir(path)        for allRenameItem in allRenameList:            renamePath =os.path.join(path,allRenameItem)            if os.path.isfile(renamePath)==True:                print allRenameItem                if allRenameItem.find('.')<0:                    newname = allRenameItem+'index.HTML'                    os.renames(os.path.join(path,allRenameItem),os.path.join(path,newname))                    print allRenameItem,'ok'                else:                    os.renames(os.path.join(path,'index.HTML'))                    print allRenameItem,'ok'                        def renamefile2(self,path,newname):        '''        批量修改文件名称,解决了中文文件名称的问题            :param path:路径        :param newname:新名称        '''            allRenameList = os.Listdir(path)        for allRenameItem in allRenameList:            renamePath =os.path.join(path,allRenameItem)            if sys.getfilesystemenCoding()=='mbcs':                renamePath=renamePath.decode('mbcs')                print renamePath            elif sys.getfilesystemenCoding() & 0x800:                renamePath=renamePath.decode('utf-8')#                 print renamePath            if os.path.isfile(renamePath)==True:                if sys.getfilesystemenCoding()=='mbcs':                    allRenameItem=allRenameItem.decode('mbcs')                elif sys.getfilesystemenCoding() & 0x800:                    allRenameItem=allRenameItem.decode('utf-8')                    if allRenameItem.find('.')<0:                        newname = allRenameItem+newname                        os.renames(os.path.join(path,newname))                        print allRenameItem,'ok'                    else:                        os.renames(os.path.join(path,newname))                        print allRenameItem,'ok'            else:                option = fileOption()                option.renamefile2(renamePath,newname)                def IteratrFolder(self,testpath):        '''        遍历文件夹 并且解决了中文文件名的问题        查看系统文件名编码  sys.getfilesystemenCoding()        :param path:        '''        ListsubDir=[]        List = os.Listdir(testpath)        for filename in List:            if sys.getfilesystemenCoding()=='mbcs':                filename=filename.decode('mbcs')                paths = os.path.join(testpath,filename)                if os.path.isdir(paths):                    ListSub = os.Listdir(paths)                    for sub in ListSub:                        subDir = os.path.join(paths,sub)                        ListsubDir.append(subDir)                return ListsubDir                        def RemovefilesByfileType(self,fileType):        '''        批量删除指定文件夹下指定文件类型的文件        :param path:路径        :param fileType:文件类型        '''        fileList = os.Listdir(path)        for one in fileList:            print one            removePath =os.path.join(path,one)            if os.path.isfile(removePath)==True:                if removePath.find(fileType)>=0:                    os.remove(removePath)         def isSubString(self,subStrList,string):        '''        判断 字符串Str是否包含序列subStrList 中的每一个子字符串        subStrList =['py','java','txt']        string = 'nihaojavaandpython.txt'        isSubString(subStrList,string)        reutn Treu(or False)                '''          flag = True        for substr in subStrList:            if not (substr in string):                flag = False                return flag        def getfileSuffix(self,filePath):        '''        得到所有文件的后缀名,用系统的函数os.path.splitext        '''        try:                        for (dirPath,dirs,files) in os.walk(filePath):                for filename in files:                    ext = os.path.splitext(filename)[1] #取得文件类型,注意它还带着点号                       print ext        except os.error,e:            print 'error e'%e          def getfileListBySuffix(self,filePath,fileList,suffixStr=[]):        '''        获取目录中指定的后缀名的文件     (自己写的方法)          '''        filenames = os.Listdir(filePath)        for filename in filenames:            findpath = os.path.join(filePath,filename)             if os.path.isfile(findpath):                if (len(filenames)>0):                    if(len(suffixStr)>0):                        #返回指定类型的文件名                        option = fileOption()                        if (option.isSubString(suffixStr,filename)):                            fullfilename = os.path.join(filePath,filename)                            fileList.append(fullfilename)                                                       elif os.path.isdir(findpath):                option = fileOption()                option.getfileListBySuffix(findpath,suffixStr)                      #对文件名排序                   if (len(fileList)>0):            fileList.sort()                        return  fileList            def getfileListBySuffix_Better(self,suffixStr=[]):        '''        获取目录中指定的后缀名的文件     (自己写的方法速度更快)          '''        for (dirPath,files) in os.walk(filePath):            for filename in files:                if (len(files)>0):                    if(len(suffixStr)>0):                        #返回指定类型的文件名                        option = fileOption()                        if (option.isSubString(suffixStr,filename)):                            fullfilename = os.path.join(filePath,filename)                            fileList.append(fullfilename)                                              #对文件名排序                   if (len(fileList)>0):            fileList.sort()            return  fileList        def getfileListBySuffix_Best(self,files) in os.walk(filePath):            for filename in files:                if (len(files)>0):                    if(len(suffixStr)>0):                        #返回指定类型的文件名                        for suf in suffixStr:                            if os.path.splitext(filename)[1].find (suf)==0:                                fullfilename = os.path.join(dirPath,filename)                                fileList.append(fullfilename)                                              #对文件名排序                   if (len(fileList)>0):            fileList.sort()            return  fileList            def copyfiles(self,sourceDir,targetDir):         '''        实现对文件的拷贝        '''        for file in os.Listdir(sourceDir):             sourcefile = os.path.join(sourceDir,file)             targetfile = os.path.join(targetDir,file)             if os.path.isfile(sourcefile):                 if not os.path.exists(targetDir):                      os.makedirs(targetDir)                  if not os.path.exists(targetfile) or(os.path.exists(targetfile) and (os.path.getsize(targetfile) != os.path.getsize(sourcefile))):                      open(targetfile,"wb").write(open(sourcefile,"rb").read())             if os.path.isdir(sourcefile):                 First_Directory = False                create = fileOption()                 create.copyfiles(sourcefile,targetfile)                    def movefiles(self,dir,extname):        '''        :param dir:原始数据目录        :param extname:文件的扩展名        根据文件名创建文件夹,并将其放入对应的文件夹内        '''        self.dir = dir        self.extname = extname        for i in os.Listdir(dir):            name = ''.join(i.split(extname)[0:-1])            print name            os.mkdir(os.path.join(dir,name))            os.rename(os.path.join(dir,i),os.path.join(dir,name,i))                                def moveDirAndfiles(self,oldpath,newPath):        '''        实现对指定文件的迁移        :param oldpath:源路径        :param newPath:目的路径        '''        self.oldpath = oldpath        self.newPath = newPath        try:            allChangefileList = os.Listdir(oldpath)            for allChangeItem in allChangefileList:                changefilePath =os.path.join(oldpath,allChangeItem)                if os.path.isdir(changefilePath):                    dstAddpath = os.path.join(newPath,allChangeItem)                    if os.path.exists(dstAddpath):                        movefiles = fileOption()                        movefiles.moveDirAndfiles(oldpath,dstAddpath)                    else:                        os.makedirs(newPath)                        os.rename(oldpath,newPath)                else:                    shutil.move(oldpath,newPath)                return True        except os.errno,e:            print 'error e%s'%e                                    #     def moveDirAndfiles_Better(self,newPath):#         '''#         实现对指定文件的迁移#         :param oldpath:源路径#         :param newPath:目的路径#         '''#         self.oldpath = oldpath#         self.newPath = newPath#         try:#             for (dirPath,files) in os.walk(oldpath):#                 for filename in files:#                     dstPath = os.path.join(dirPath,filename)#                     print dstPath#                     if os.path.exists(dstPath):#                         shutil.move(oldpath,newPath)#                     else:#                         os.makedirs(newPath)#                         os.rename(oldpath,newPath)#         except os.errno,e:#             print 'error e%s'%e    def TraverseFolder(self,path):        '''        遍历文件夹        :param path:        '''        self.path = path        print "<----folder&file---->"        no=0        for (path,files) in os.walk(path):            print             no += 1            print "No.%d"%no            print "path=%s"%path            if len(dirs)!=0:#                 print type(dirs)                subfolders = ''                for dir in dirs:                    subfolders += dir+';'                subfolders = '[' + subfolders + ']'                print "subfolders=%s"%subfolders            if len(files)!=0:                filenames = ''                for filename in files:                    filenames += filename+';'                filenames = '[' + filenames + ']'                print "files=%s"%filenames        print "<----folder&file---->"            def renameExpendnameOffile(self,oldexp_name,newexp_name):        '''                :param path:路径        :param oldexp_name:旧的扩展名        :param newexp_name:新的扩展名        '''        self.path = path        self.oldexp_name = oldexp_name        self.newexp_name = newexp_name        changedCount = 0                  for (path,files) in os.walk(path):            for filename in files:                ext = os.path.splitext(filename)[1] #取得文件类型,注意它还带着点号                            if (ext == oldexp_name):                    changedCount += 1                    newname = filename.replace(oldexp_name,newexp_name)                    oldpath = path + "\" + filename                          newpath = path + "\" + newname                    try:                        os.rename(oldpath,newpath)                        print 'No.%d'%changedCount,'change',oldpath,'to',newpath                    except BaseException,e:                          print(str(e))                    def renameExpendnameOffile2(self):        '''    修改文件的后缀名,有控制台输入参数    '''        str = u'请输入要处理的文件夹路径====>'        path = raw_input(str.encode('utf-8'))        print path            str = u'请输入源文件类型(不包括.)====>'        old_ext = "."+raw_input(str.encode('utf-8'))        print old_ext            str = u'请输入目标文件类型(不包括.)====>'        new_ext = "."+raw_input(str.encode('utf-8'))            print new_ext            print   #输出空行占位        f = fileOption()        f.TraverseFolder(path)        print            str = u'开始批量更名'        print str        print '<-----------------'        changedCount = 0                  for (path,files) in os.walk(path):            for filename in files:                ext = os.path.splitext(filename)[1] #取得文件类型,注意它还带着点号                            if (ext == old_ext):                    changedCount += 1                    newname = filename.replace(old_ext,new_ext)                    oldpath = path + "\" + filename                          newpath = path + "\" + newname                    try:                        os.rename(oldpath,e:                          print(str(e))        print '----------------->'    def deleteFolder(self,path):        '''        删除指定目录下的文件        :param path:        '''        self.path = path                for root,files in os.walk(path,False):            for name in files:                os.remove(os.path.join(root,name))            for name in dirs:                os.rmdir(os.path.join(root,name))                                                    def remove_empty_dir(self,path):        '''        删除目录下所有的空文件夹        :param path:        '''        self.path = path                while(path[-1] == "\"):            path = path[:-1]            print path         a = {}        for root,False):            print dirs            if len(files) == 0:                a[root] = 0            else:                for file in files:                    try:                        fn = os.path.join(root,file)                        size = os.path.getsize(fn)                        if size != 0:                            b = root                            while(b != path):                                a[b] = 1                                b = b.rpartition("\")[0]                            a[path] = 1                        else:                            try:                                os.remove(fn)                                a[root] = 0                            except (windowsError):                                b = root                                while(b != path):                                    a[b] = 1                                    b = b.rpartition("\")[0]                                a[path] = 1                    except (windowsError):                        b = root                        while(b != path):                            a[b] = 1                            b = b.rpartition("\")[0]                        a[path] = 1                                         if a[root]:                        break;        empty_dirs = []        for i,j in a.iteritems():            if j == 0:                print i                empty_dirs.insert(0,i)        del a        empty_dirs.sort(reverse = True)            for i in empty_dirs:            try:                os.rmdir(i)                print "%s 删掉了!!" % (i)            except (windowsError):                print "%s 删不掉!!" % (i)                    def zipAllfiles(self,startdir,zipPath):        '''    实现批量压缩文件        :param startdir: 压缩包的层次  比如download.zip解压开是:download/1/1.HTML   所以startdir 就是‘f:/download/’        :param zipPath:  download/1/ 文件的上次层次        '''        self.startdir = startdir        self.zipPath = zipPath                for dirpath,dirnames,filenames in os.walk(startdir):            for filename in filenames:                newp = dirpath+'\'                if newp.find('F:'+zipPath)==0:                    print os.path.join(newp,filename)             #                     f = zipfile.Zipfile('F:\pythonTest\zipTest\'+'download'+str(DownID)+'.zip','a',zipfile.ZIP_DEFLATED)#                     f.write(os.path.join(newp,filename))#                     print os.path.join(newp,filename)#                     f.close()                                            def getAllfileSize(self,size):        '''        得到整个文件所在文件夹的大小        1B = 8 bit        1KB = 1024 B        1MB = 1024 KB        1MB = 1024*1024 B        1GB = 1024 MB        :param path:        '''        self.path = path        for dirpath,files in os.walk(path):            size += sum([getsize(join(dirpath,name)) for name in files])        return size        def getfileSize(self,path):        '''        得到每个文件的大小,以字典方式存入List中        :param path:        '''        self.path = path                sizeDic={}        sizeList = []        for dirpath,files in os.walk(path):                        for file in files:                fileSize = getsize(join(dirpath,file))                sizeDic={file:fileSize}                sizeList.append(sizeDic)        return sizeList                                if __name__=='__main__':    db = DbUtil()    create =fileOption()    size1 = create.getfileSize('F:\pythonTest\delete\')

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的python文件 *** 作全部内容,希望文章能够帮你解决python文件 *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199431.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存