zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    五.Flink实时项目电商用户行为分析之订单支付实时监控
    四.Flink实时项目电商用户行为分析之恶意登录监控
    三.Flink实时项目电商用户行为分析之市场营销商业指标统计分析
    二.Flink实时项目电商用户行为之实时流量统计
    一.Flink实时项目电商用户行为分析之实时热门商品统计
    Flink 流处理API之实现UDF函数——更细粒度的控制流
    二.Flink 流处理API之Transform
    5组-Alpha冲刺-1/6
    5组 需求分析报告
    5组 团队展示
  • 原文地址:https://www.cnblogs.com/countryboy666/p/14501651.html
Copyright © 2011-2022 走看看