zoukankan      html  css  js  c++  java
  • 程序文件路径和目录的操作之BASEDIR目录获取

    import sys
    import os
    BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    #通过py所在的文件找到他的绝对路径
    # os.path.abspath(__file__)
    # 通过os.path.dirname(os.path.abspath(__file__))找到父目录
    # os.path.dirname(os.path.dirname(os.path.abspath(__file__)))找到爷爷目录
    sys.path.append(BASE_DIR)
    from mudule import main
     
    main.main()

    思路通过要执行的py文件通过

    os.path.abspath(__file__)
    拿到文件所在的绝对路径

    通过os.path.dirname(os.path.abspath(__file__))找到父目录

    os.path.dirname(os.path.dirname(os.path.abspath(__file__)))找到爷爷目录

    作用:

    解决:Python运行过程中模块的调用常常在当前的路径,这样移植程序之后,会发生找不到文件,是因为没有指定到执行目录

    pycharm运行不报错的原因是,他会自动添加对应的目录
    ————————————————
    版权声明:本文为CSDN博主「测试的世界很精彩」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/pacermaker/article/details/101700689

  • 相关阅读:
    linux 文件类型 文件权限
    微信公众号支付
    struts2 详解
    git 命令行操作
    javascript 闭包
    SVN 基本操作
    javascript 函数 方法
    git
    javascript变量 数组 对象
    Intellij调试debug
  • 原文地址:https://www.cnblogs.com/softtester/p/11608805.html
Copyright © 2011-2022 走看看