httprunner的env里,有空格和注释报错问题解决

.env

testenv=xxxxx

1.当文件内包含注释或空行时,抛出异常 httprunner.exceptions.FileFormatError: .env format error
原因是源代码中没有对空行和 # 号做处理,代码片段 (loader - 130):

with open(dot_env_path, mode="rb") as fp:
    for line in fp:
        # maxsplit=1
        if b"=" in line:
            variable, value = line.split(b"=", 1)
        elif b":" in line:
            variable, value = line.split(b":", 1)
        else:
            raise exceptions.FileFormatError(".env format error")

2.加上判断忽略掉注释和空行,就不会报错了

with open(dot_env_path, mode="rb") as fp:
    for line in fp:
        # maxsplit=1
        line = line.strip()
        if not len(line) or line.startswith(b"#"):
            continue
        if b"=" in line:
            variable, value = line.split(b"=", 1)
        elif b":" in line:
            variable, value = line.split(b":", 1)
        else:
            raise exceptions.FileFormatError(".env format error")
上一篇:DAY 205 python使用ftplib模块实现FTP文件的上传下载


下一篇:2021-2022-1 20191315《信息安全系统设计与实现(上)》学习笔记2