• python 获取运行脚本和模块的绝对路径


    方法一:sys.args[0]

    在python的运行时,sys.argv[0],存了当前脚本的运行路径包括文件名

    python test.py  
    则:sys.argv[0] =>test.py
    
    python dirname1/dirname2/test.py  
    则:sys.argv[0] =>dirname1/dirname2/test.py
    
    python /centos/home/test.py  
    则 sys.argv[0] =>/centos/home/test.py

    方法二:使用__file__

    print(__file__)
    C:/Users/WQBin/PycharmProjects/pyMibXgo/daydaywork/creidt 表历史存档/test4.py
    import pymongo
    
    print(pymongo.__file__)
    
    D:appAnacondalibsite-packagespymongo__init__.py

    方法三:使用abspath和getcwd()

        def abspath(path):
            """Return the absolute version of a path."""
    
            if path: # Empty path must return current working directory.
                path = os.fspath(path)
                try:
                    path = _getfullpathname(path)
                except OSError:
                    pass # Bad path - return unchanged.
            elif isinstance(path, bytes):
                path = os.getcwdb()
            else:
                path = os.getcwd()
            return normpath(path)

     完结!!

  • 相关阅读:
    链式栈的C++实现
    Java面试之设计模式二
    前端资源
    Java面试之异常
    Java面试之序列化
    Java面试之重写(Override)与重载(Overload)
    项目视图展示
    Java面试之集合
    Java面试之SSH框架面试题集锦
    JDBC技术
  • 原文地址:https://www.cnblogs.com/wqbin/p/11983894.html
Copyright © 2020-2023  润新知