python文件操作

功能:

文件移动,文件拷贝,文件重命名 ,删除文件, 压缩文件

 

import os
import shutil
import zipfile

class MyFileHandle:
    def __init__(self):
        pass

    def movePath(self,src,dst):
        src = os.path.join(os.path.abspath("../%s"%src)) # 获取文件路径
        if os.path.isfile(dst):
            raise TypeError("dst path error")
            return -1
        if not os.path.exists(src):
            raise TypeError("src could not find src path %s" %src)
            return -1
        if os.path.isfile(src):
            fileName = os.path.basename(src) # 文件名字
            shutil.move(src,dst + '/' + fileName)
        else:
            shutil.move(src,dst) # 文件夹

    def copyPath(self, src, dst):
        if os.path.isfile(dst):  # 文件
            raise TypeError('plPath dst path error')
            return -1
        if not os.path.exists(dst):  # 不存在
            os.mkdir(dst)

        if not os.path.exists(src):  # 不存在资源文件
            raise TypeError('plPath could not find src path(%s)' % src)
            return -1

        if os.path.isfile(src):  # 资源文件是一个文件
            fileName = os.path.basename(src)
            shutil.copy(src, dst + '/' + fileName)
        else:
            shutil.copytree(src, dst)   # 拷贝文件夹

    def renameFile(self, srcName, newName):  # 重命名文件
        if not os.path.isfile(srcName):     # 判断文件是不是存在
            raise TypeError('plPath dst path error')
            return -1
        else:
            newPath = os.path.dirname(srcName) + '/' + newName  # 获取文件夹路径,
            shutil.move(srcName, newPath)

    def backupFile(self, srcFile, fixName, dstPath): # 备份文件
        if not os.path.isfile(srcFile):
            raise TypeError('plPath srcFile error')
            return -1
        if os.path.isfile(dstPath):  # 判断是不是文件
            raise TypeError('plPath dstPath error')
            return -2
        if not os.path.exists(dstPath):
            os.mkdir(dstPath)

        self.copyPath(srcFile, dstPath)  # 拷贝文件
        newFile = dstPath + '/' + os.path.basename(srcFile)
        newName = fixName + '_' + os.path.basename(srcFile)
        self.renameFile(newFile, newName)  # 重命名

    def deletePath(self, src):
        src = os.path.join(os.path.abspath("../%s" % src))
        if not os.path.exists(src):
            raise TypeError('plPath could not find this file(%s)' % src)
            return None
        if os.path.isfile(src):
            os.unlink(src)  # 删除文件
        else:
            shutil.rmtree(src)  # 递归删除目录树。

    def getPathFiles(self, srcPath):
        files = []
        srcPath = os.path.join(os.path.abspath("../%s" % srcPath))
        if os.path.isfile(srcPath):
            if os.path.exists(srcPath):
                return [srcPath]
            else:
                raise TypeError('plPath could not find this file(%s)' % srcPath)
                return None
        else:
            if not os.path.exists(srcPath):
                os.mkdir(srcPath)
                raise TypeError('plPath could file in this path (%s)' % srcPath)
                return None
            else:
                dir_list = os.listdir(srcPath)
                for file in dir_list:
                    path = os.path.join(srcPath,file)
                    if os.path.isfile(path):
                        print(file)
                        files.append(file)

            return files

    def getPathFilesByKey(self, srcPath, key):
        srcPath = os.path.join(os.path.abspath("../%s" % srcPath))
        files = []
        if os.path.isfile(srcPath):
            if os.path.exists(srcPath):
                return [srcPath]
            else:
                raise TypeError('plPath could not find this file(%s)' % srcPath)
                return None
        else:
            if not os.path.exists(srcPath):
                raise TypeError('plPath could file in this path (%s)' % srcPath)
                return None
            else:
                dir_list = os.listdir(srcPath)
                for file in dir_list:
                    if key == 'olderFile':
                        file_List = file.split('.')
                        if len(file_List) == 3:
                            files.append(file)
                    else:
                        path = os.path.join(srcPath, file)
                        if os.path.isfile(path):
                            if file.find(key) >= 0:
                                files.append(file)
                return files

    def zipFile(self, srcPath, dstZipFile):  # 压缩文件  Zip文件压缩方法的常量
        '''
        :param srcPath: 文件夹或则文件
        :param dstZipFile:  目标文件
        :return: 
        '''
        f = zipfile.ZipFile(dstZipFile, 'w', zipfile.ZIP_DEFLATED)
        if os.path.isfile(srcPath):  # 如果是文件
            f.write(srcPath)
        else:
            for dirpath, dirnames, filenames in os.walk(srcPath): # 遍历文件夹
                fpath = dirpath.replace(srcPath, '') #
                fpath = fpath and fpath + os.sep or ''
                for filename in filenames: # 将filename中的字节放入归档文件的名称下弧名
                    f.write(os.path.join(dirpath, filename), fpath + filename)

        f.close()

 

posted @ 2021-03-08 19:30  countryboy666  阅读(30)  评论(0编辑  收藏  举报