• python3的一些文件操作的脚手架


    用python把原来的脚本重构了一下,其中写了文件操作的一些函数,如下:

    import os
    import shutil
    import hashlib
    import stat
    
    #查找文件夹中的某个文件
    def findMyFileDir(dirPath, findFile):
        files = []
        dirs = []
        for root, dirs, files in os.walk(dirPath, topdown=False):
            for file in files:
                if file == findFile:
                    return root
            for dir in dirs:
                findMyFileDir(os.path.join(root, dir), findFile)
    
    #创建一个文件夹
    def createDir(dirPath):
        os.makedirs(dirPath, exist_ok=True)
    
    #删除一个文件
    def delFile(filePath):
        if os.path.exists(filePath):
            os.remove(filePath)
    
    #删除文件夹里所有的文件
    def delDir(dir):
        if(os.path.isdir(dir)):
            for f in os.listdir(dir):
                delDir(os.path.join(dir, f))
            if(os.path.exists(dir)):
                os.rmdir(dir)
        else:
            if(os.path.exists(dir)):
                os.remove(dir)
    
    #拷贝文件
    def copyFile(sourceFilePath, destFilePath):
        if not(os.path.exists(sourceFilePath)):
            return False
    
        if os.path.exists(destFilePath):
            if getFileMd5(sourceFilePath) == getFileMd5(destFilePath):
                return True
            else:
                os.remove(destFilePath)
    
        destFileDir = os.path.dirname(destFilePath)
        os.makedirs(destFileDir, exist_ok=True)
        if not(shutil.copyfile(sourceFilePath, destFilePath, follow_symlinks=False)):
            return False            
        return True
    
    #拷贝文件夹里的文件
    def copyDir(sourceDir, destDir):
        if not(os.path.exists(sourceDir)):
            return False
    
        if os.path.exists(destDir):
            shutil.rmtree(destDir)
    
        if not(shutil.copytree(sourceDir, destDir, symlinks=True)):
            return False
        return True
    
    #获取文件的md5
    def getFileMd5(filePath):
        with open(filePath, 'rb') as f:
            content = f.read()
        hash = hashlib.md5()
        hash.update(content)
        return hash.hexdigest()
    
    #获取一个文件夹里的所有的文件和该文件对应的md5
    def dirList(dirPath):
        listDict = {}
        files = []
        dirs = []
        for root, dirs, files in os.walk(dirPath, topdown=False, followlinks=True):
            for file in files:
                filePath = os.path.join(root, file)
                listDict[os.path.relpath(filePath, dirPath).replace(
                    '\', '/')] = getFileMd5(filePath)
        for dir in dirs:
            dirList(os.path.join(root, dir))
        return listDict
    
    #逐行读一个文件,并过来文件中某些行里回车和空格    
    def readLineForFile(filePath):
        f = open(filePath, 'r')   
        lines = f.readlines()
        f.close()
        newLines = []
        for line in lines:
            line = line.replace('
    ', '').strip()
            if line:
                newLines.append(line)
        return newLines
  • 相关阅读:
    用Struts2框架报错:The Struts dispatcher cannot be found
    Struts2.0笔记二
    [转]使用Struts 2防止表单重复提交
    Struts配置文件报错"元素类型为 "package" 的内容必须匹配"
    Java基础知识
    Struts笔记二:栈值的内存区域及标签和拦截器
    [转]迭代器
    Struts笔记一
    账户注册激活邮件及登入和注销
    EL表达式获取对象属性的原理
  • 原文地址:https://www.cnblogs.com/lmh001/p/9997766.html
Copyright © 2020-2023  润新知