zoukankan      html  css  js  c++  java
  • python获取当前文件路径以及父文件路径

    1
    2
    3
    4
    5
    6
    #当前文件的路径
    pwd = os.getcwd()
    #当前文件的父路径
    father_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".")
    #当前文件的前两级目录
    grader_father=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"..")
     

    第一种方法:

    os.path.abspath(__file__)

    假设app.py中想读取config.ini文件的内容,首先app.py需要知道config.ini的文件路径,从目录结构上可以看出,config.ini与app.py的父目录同级,也就是获取到app.py父目录(bin文件夹的路径)的父目录(config文件夹路径)的绝对路径再拼上config.ini文件名就能获取到config.ini文件

    首先,在app.py中测试一下:

    import os
    
    def load_file():
    # 获取当前文件路径
    current_path = os.path.abspath(__file__)
    # 获取当前文件的父目录
    father_path = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")
    # config.ini文件路径,获取当前目录的父目录的父目录与congig.ini拼接
    config_file_path=os.path.join(os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".."),'config.ini')
    print('当前目录:' + current_path)
    print('当前父目录:' + father_path)
    print('config.ini路径:' + config_file_path)
    
    
    load_file()
    

      


    输出结果:

    当前目录:/Users/shanml/Documents/python/config/bin/app.py
    当前父目录:/Users/shanml/Documents/python/config/bin
    config.ini路径:/Users/shanml/Documents/python/config/config.ini
    从结果中可以看到一切都正常,没有什么问题,假如现在需要从main.py中执行app.py的load_file()方法呢?
    来测试一下:

    main.py
    
    from bin.app import load_file
    
    if __name__=='__main__':
    load_file()
    

      


    输出结果,路径同样没问题:

    当前目录:/Users/shanml/Documents/python/config/main.py
    当前父目录:/Users/shanml/Documents/python/config
    config.ini路径:/Users/shanml/Documents/python/config.ini
    参考:https://www.cnblogs.com/yajing-zh/p/6807968.html

    第二种方法:
    使用inspect

    app.py:

    import os,inspect
    
    
    def load_file():
    # 获取当前文件路径
    current_path=inspect.getfile(inspect.currentframe())
    # 获取当前文件所在目录,相当于当前文件的父目录
    dir_name=os.path.dirname(current_path)
    # 转换为绝对路径
    file_abs_path=os.path.abspath(dir_name)
    # 划分目录,比如a/b/c划分后变为a/b和c
    list_path=os.path.split(file_abs_path)
    print('list_path:' + str(list_path))
    # 配置文件路径
    config_file_path=os.path.join(list_path[0],'config.ini')
    print('当前目录:' + current_path)
    print('config.ini文件路径:' + config_file_path)
    

      


    在app.py中执行load_file()方法:

    list_path:('/Users/shanml/Documents/python/config', 'bin')
    当前目录:/Users/shanml/Documents/python/config/bin/app.py
    config.ini文件路径:/Users/shanml/Documents/python/config/config.ini

    在mian.py中执行load_file方法:

    list_path:('/Users/shanml/Documents/python/config', 'bin')
    当前目录:/Users/shanml/Documents/python/config/bin/app.py
    config.ini文件路径:/Users/shanml/Documents/python/config/config.ini

    参考:https://xnow.me/programs/python.html
    ---------------------
    作者:S_H-A_N
    来源:CSDN
    原文:https://blog.csdn.net/lom9357bye/article/details/79285170
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    Pycharm在线/手动离线安装第三方库-以scapy为例(本地离线添加已经安装的第三方库通过添加Path实现)
    python+splinter实现12306网站刷票并自动购票流程
    利用RELK进行日志收集
    web安全之文件上传漏洞攻击与防范方法
    C# 使用 CancellationTokenSource 终止线程
    ASP.NET MVC 下拉框的传值的两种方式
    SQL Server(解决问题)已成功与服务器建立连接,但是在登录过程中发生错误。(provider: Shared Memory Provider, error:0
    C# .net中json字符串和对象之间的转化方法
    asp.net 未能加载文件或程序集“WebApi”或它的某一个依赖项。试图加载格式不正确的程序。
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/sddai/p/10408524.html
Copyright © 2011-2022 走看看