mac python3获取当前脚本路径

 

脚本使用环境:Mac osX

使用变量  __file__    

代码如下:

#!/usr/bin/python3

abs_file    = __file__
# output current file's directory, like: '/aaa/xxx/Desktop/ex.py'
print(abs_file) 

# get path , like: '/aaa/xxx/Desktop'
abs_file    = abs_file[:abs_file.rfind('/')]
print(abs_file)

输出:

/Users/666/new/python3/ex/ex1.py
/Users/666/new/python3/ex

 

你可能感兴趣的:(python3)