zoukankan      html  css  js  c++  java
  • 获取文件版本号

    原因:上线时总是需要统计本次上线dll的版本号,一个个看属性太费劲了,用python实现查看文件的版本号,写入excel

    步骤:1、遍历目录下所有文件,

       2、得到文件的版本号,修改时间

       3、写入excel

    import time
    from common import Common
    from excelCommon import ExcelCommon
    
    class Test(object):
    
        def __init__(self):
            pass
        # 获取文件
        def getFileDate(self,path):
            fileDate = Common.getFileDict(self,path)
            return fileDate
    
        #获取文件大小
        def getFileSize(self,path):
            fileSize = Common.getFileSize(self,path,"kb")
            return str(fileSize) + "kb"
    
        # 获取文件版本号
        def getFileVersion(self,path):
            fileVersion = Common.getFileVersion(self,path)
            return fileVersion
    
        # 文件修改时间
        def getFileUpdate(self,path):
            # 原始时间
            befortime = Common.getFileUpdate(self,path)
            # 原时间转换为元组时间
            fileUpdate = time.strptime(befortime)
            # 元组时间格式化
            filetime = time.strftime("%Y/%m/%d %H/%M/%S",fileUpdate)
            return filetime
    
        # 文件版本、大小、修改时间,添加至list<map>
        def control(self, path):
            filedata = self.getFileDate(path)
            for item in filedata:
                fileVersion = self.getFileVersion(item["fileabspath"])
                fileSize = self.getFileSize(item["fileabspath"])
                fileUpdate = self.getFileUpdate(item["fileabspath"])
                item.setdefault("fileVersion",fileVersion)
                item.setdefault("fileSize",fileSize)
                item.setdefault("fileUpdate",fileUpdate)
            return filedata
    
        # 数据写入excel表格
        def write_excel(self,xlsName,sheetName,data,title,itemTitle):
            ExcelCommon.excel_write(self,xlsName,sheetName,data,title,itemTitle)
    
    if __name__ == '__main__':
        test = Test()
        xlswriter = ExcelCommon()
        data = test.control(r"D:DiskTop	est")
        title = ["文件路径","文件名称","文件版本","文件大小","修改日期"]
        itemTitle = ["dirpath","filename","fileVersion","fileSize","fileUpdate"]
        test.write_excel("file.xlsx","dll",data,title,itemTitle)
    import os
    import shutil
    import math
    import win32api
    import time
    
    class Common(object):
    
        def __init__(self):
            pass
    
        # 获取指定文件夹下所有文件绝对路径 返回数组
        '''
        dirpath     是一个string,代表目录的路径,
        dirnames    是一个list,包含了dirpath下所有子目录的名字。
        filenames   是一个list,包含了非目录文件的名字。
        '''
        def getFileList(self, path):
            file_list = []
            for (dirpath, dirnames, filenames) in os.walk(path):
                for file in filenames:
                    file_list.append(os.path.join(dirpath, file))
            return file_list
    
        # 获取文件夹下所有文件 返回list<map>
        '''
            [{
                'dirpath': 'D:\DiskTop\test\01_优化 读取配置文件更换为数据库查询 功能',
                'filename': 'filTSALDIInventoryUpload.exe'
             }, {
                'dirpath': 'D:\DiskTop\test\01_优化 读取配置文件更换为数据库查询 功能',
                'filename': 'filTSALDIInventoryUpload.xml'
             }]
        '''
        def getFileDict(self, path):
            fileList = []
            for (dirpath, dirname, filename) in os.walk(path,os.sep):
                for file in filename:
                    map = {
                        "dirpath":dirpath,
                        "filename": file,
                        "fileabspath": os.path.join(dirpath,file)
                    }
                    fileList.append(map)
            return fileList
    
        # 判断文件夹是否存在 存在:删除该文件夹下所有内容并创建,不存在:创建文件夹
        # 参数:检查目录、检查名字
        def isExist(self, path, fileName):
            os.chdir(path)
            if os.path.exists(fileName):
                shutil.rmtree(fileName)
                os.makedirs(fileName)
            else:
                os.makedirs(fileName)
    
        # 获取文件大小
        def getFileSize(self,path,size):
            fileSize = os.path.getsize(path)
            if size == "kb" or size == "KB":
                fileSize = math.ceil(fileSize / 1024)
                return fileSize
    
        # 获取文件版本号
        def getFileVersion(self,path):
            try:
                info = win32api.GetFileVersionInfo(path, os.sep)
                ms = info['FileVersionMS']
                ls = info['FileVersionLS']
                version = '%d.%d.%d.%d' % (win32api.HIWORD(ms), win32api.LOWORD(ms), win32api.HIWORD(ls), win32api.LOWORD(ls))
            except:
                version = ""
            return version
    
        # 获取文件更新时间
        """
            # os.path.getatime(file) 输出文件访问时间
            # os.path.getctime(file) 输出文件的创建时间
            # os.path.getmtime(file) 输出文件最近修改时间
        """
        def getFileUpdate(self,path):
            return time.ctime(os.path.getmtime(path))
    
    
    if __name__ == '__main__':
        common = Common()
        # files = common.getFileList("D:\DiskTop\new")
        # common.isExist("D:DiskTop\new", "20201231")
        # files = common.getFileDict(r"D:DiskTop	est")
        # print(files)
    
        time = common.getTime(time.localtime(time.time()))
        print(time)
    import xlsxwriter
    
    class ExcelCommon(object):
    
        def __init__(self):
            pass
    
        def excel_write(self,xlsName,sheetName,data,title,itemTitle):
            workbook = xlsxwriter.Workbook(xlsName)
            sheet = workbook.add_worksheet(sheetName)
            if title:
                # 写入第一行
                for index, item in enumerate(title):
                    sheet.write(0, index, item)
                row = 1
            else:
                row = 0
            for item in data:
                for index,i in enumerate(itemTitle):
                    sheet.write(row,index,item.get(i))
                row += 1
    
            workbook.close()
    
    if __name__ == '__main__':
        excelCommon = ExcelCommon()
        data = [{
        'dirpath': 'D:\DiskTop\test\01_优化 读取配置文件更换为数据库查询 功能',
        'filename': 'filTSALDIInventoryUpload.exe',
        'fileabspath': 'D:\DiskTop\test\01_优化 读取配置文件更换为数据库查询 功能\filTSALDIInventoryUpload.exe',
        'fileVersion': '6.0.77.14',
        'fileSize': '37kb',
        'fileUpdate': 'Thu Jan 14 17:06:36 2021'
    }]
        vtitle = ["dirpath",'fileVersion','fileSize']
        title = ["路径",'文件名','文件大小']
    
        excelCommon.excel_write("test.xlsx","dll",data,title,vtitle)
  • 相关阅读:
    VS2010 LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 的解决方法
    Navicat Premium11.0.16 for mac 中文破解版
    angular input输入框中使用filter格式化日期
    Mac下搭建Eclipse Android开发环境
    mac下修改.bash_profile立即生效的方法
    Ionic ngMessage 表单验证
    mongodb授权登录
    Ionic开发之条形码扫描
    ionic 到真相后$http.get()无法请求,导致空白的情况,如何解决
    Xcode 7中http通信出现如下错误:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
  • 原文地址:https://www.cnblogs.com/lfotest/p/14288909.html
Copyright © 2011-2022 走看看