获取当前脚本所在的目录和路径

用不同的方式, 获取当前文件file的路径

import os,sys
​
if __name__=="__main__":
    print(__file__)
    print(os.path.realpath(__file__))
    print(os.path.abspath(__file__))
    print(sys.argv[0])
获取当前脚本所在路径 

用不同的方式, 来获取当前脚本所在的目录

import os,sys
if __name__=="__main__":
    print(os.path.dirname(os.path.realpath(__file__)))
    print(os.path.split(os.path.realpath(__file__))[0])
    print(os.getcwd())
    print(sys.path[0])
 
获取当前脚本所在目录

请使用手机"扫一扫"x

原文地址:https://www.cnblogs.com/amou/p/9599647.html