5.6. Reading Versions from FileΒΆ

In cases where a version is stored inside a file, one possible solution is to use the following function:

import os
from typing import Union
from semver.version import Version

def get_version(path: Union[str, os.PathLike]) -> semver.Version:
    """
    Construct a Version object from a file

    :param path: A text file only containing the semantic version
    :return: A :class:`Version` object containing the semantic
             version from the file.
    """
    version = open(path,"r").read().strip()
    return Version.parse(version)