1. 程式人生 > >Python os.path.abspath(__file__) 獲取指令碼完整路徑

Python os.path.abspath(__file__) 獲取指令碼完整路徑

import os
import sys

print os.path.abspath(__file__)  

在Python控制檯下,直接使用print __file__是會導致  name ‘__file__’ is not defined錯誤的,因為這時沒有在任何一個指令碼下執行,自然沒有 __file__的定義了。