在寫python程序中,有可能需要獲取當前運行腳本的路徑。打包成exe的腳本和直接運行地腳本在獲取路徑上稍微有點不同。
import os
import sys
# determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)