获取当前脚本所在的绝对路径

os.path.split(os.path.realpath(__file__))[0]
详解:
__file__:python内部变量,显示文件在当前搜索路径下的相对路径与文件名
os.path.realpath():获取文件的绝对路径
os.path.split():拆分最后一个路径后的内容,形成一个List对象
[0]:取列表对象的第一个参数,即为路径
 
测试程序:
import os
print(__file__)
print(os.path.realpath(__file__))
pwd=os.path.split(os.path.realpath(__file__))
print(pwd)
posted @ 2017-08-02 09:47  fosonR  阅读(741)  评论(0)    收藏  举报