python文件操作
2018-07-20 来源:open-open
#! /usr/bin/python # -*- coding:utf-8 -*- ''' Created on 2013-12-11 @author: Java ''' import os import MySQLdb from db.DbUtil import DbUtil import time import shutil import sys import zipfile from os.path import join,getsize import math class 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,allRenameItem), 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,allRenameItem), os.path.join(path,newname)) print allRenameItem,'ok' else: os.renames(os.path.join(path,allRenameItem), 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,path,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,fileList,suffixStr) #对文件名排序 if (len(fileList)>0): fileList.sort() return fileList def getFileListBySuffix_Better(self,filePath,fileList,suffixStr=[]): ''' 获取目录中指定的后缀名的文件 (自己写的方法速度更快) ''' for (dirPath,dirs,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,filePath,fileList,suffixStr=[]): ''' 获取目录中指定的后缀名的文件 (自己写的方法速度更快) ''' for (dirPath,dirs,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,oldPath,newPath): # ''' # 实现对指定文件的迁移 # :param oldPath:源路径 # :param newPath:目的路径 # ''' # self.oldPath = oldPath # self.newPath = newPath # try: # for (dirPath,dirs,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,dirs,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,path,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,dirs,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,dirs,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, newpath) print 'No.%d'%changedCount, 'change', oldpath, 'to', newpath except BaseException, e: print(str(e)) print '----------------->' def deleteFolder(self,path): ''' 删除指定目录下的文件 :param path: ''' self.path = path for root ,dirs,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, dirs, files in os.walk(path, 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,path,size): ''' 得到整个文件所在文件夹的大小 1B = 8 bit 1KB = 1024 B 1MB = 1024 KB 1MB = 1024*1024 B 1GB = 1024 MB :param path: ''' self.path = path for dirpath,dirs,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,dirs,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\\')
标签: Mysql
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐