• Python_任意文件中获取项目的绝对路径


    使用HTMLTestRunner.py(Version 0.8.2.2-huanghe)时,遇到添加饼图的问题,由于原HTMLTestRunner_my.py文件是适配任意场景的,所以饼图这个函数DrawPie(self, result)的896行:

    firedir = r'%s/myreports/mypies/'%os.path.dirname(os.path.abspath(__file__)) 

    如果把HTMLTestRunner.py与放置饼图的目录分开存放,就会在运行主程序时遇到找不到pie.png问题。

    为了解决这问题,需要处理2个点:

    1、要知道项目根目录是什么,当然不能写死了,万一更换位置部署程序,就会出错。

    1 project_name = "InterfaceTest_project"
    2 curPath = os.path.abspath(os.path.dirname(__file__))
    3 rootPath = curPath[:curPath.find(project_name+"\")+len(project_name+"\")]  #关键点!!

    此时获取的内容是e:abccd这样的路径,print看到的是e:\abc\bcd\

    2、需要把获取到项目根路径中“”(用print看到的是"\")替换测“/”。

    1 rootPath = str(rootPath).replace("\","/")
    2 firedir='{}reports/mypies'.format(rootPath) #饼图存储的目录
    3 # 这里与HTMLTestRunner.py原代码firedir='%s/myreports/mypies'%rootpath不太一样,
    4 #用的时候自己修改一下就行
  • 相关阅读:
    初识计算机
    前端html css
    mysql高级
    mysql多表查询
    mysql数据库查询
    mysql表关系
    mysql数据类型
    mysql数据库介绍
    异步回调 协程
    GIL-全局解释器锁
  • 原文地址:https://www.cnblogs.com/kuzaman/p/11528812.html
Copyright © 2020-2023  润新知