python文件操作

em7 9年前發布 | 5K 次閱讀 Python

#! /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\')

</pre>

 本文由用戶 em7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!