【Python】版本自动控制/在上一次版本中自动生成本次的版本号

 

1、代码

直接调用 VersionControl.get_version() 即可返回本次版本号(上一次版本号+1)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Time    :2021/10/28 19:07
@Author  :维斯
@File    :VersionControl.py
@Version :1.0
@Function:
"""


class VersionControl:
    @staticmethod
    def __read_file(version_file):
        with open(version_file, 'r') as f:
            data = f.read()
            return data

    @staticmethod
    def __write_file(version_file, version):
        with open(version_file, 'w') as f:
            f.write(version)

    @staticmethod
    def __get(version):
        ver = version.replace('.', '')
        now_version = int(ver) + 1
        str_version = str(now_version).zfill(3)
        str_list = []
        for i in str_version:
            str_list.append(i)
        str_new = ""
        for i in str_list:
            str_new += i + "."
        str_new = str_new[:-1]
        return str_new

    @staticmethod
    def get_version(version_file=None):
        """
        默认文件为当前路径下的versionControl.txt文件
        @param version_file: 
        @return: 
        """
        if version_file is None:
            version_file = 'versionControl.txt'
        version = VersionControl.__get(VersionControl.__read_file(version_file))
        VersionControl.__write_file(version_file, version)
        return version


if __name__ == '__main__':
    print(VersionControl.get_version())

2、版本文件

 

 

3、结果

 

posted @ 2021-10-28 19:49  淡怀  阅读(246)  评论(0编辑  收藏  举报