Python獲取當(dāng)前路徑實(shí)現(xiàn)代碼
Python獲取當(dāng)前路徑實(shí)現(xiàn)代碼
import os,sys
使用sys.path[0]、sys.argv[0]、os.getcwd()、os.path.abspath(__file__)、os.path.realpath(__file__)
sys.path是Python會去尋找模塊的搜索路徑列表,sys.path[0]和sys.argv[0]是一回事因?yàn)镻ython會自動把sys.argv[0]加入
sys.path。
如果你在C:\test目錄下執(zhí)行python getpath\getpath.py,那么os.getcwd()會輸出“C:\test”,sys.path[0]會輸出“C:\test\getpath”。
如果你用py2exe模塊把Python腳本編譯為可執(zhí)行文件,那么sys.path[0]的輸出還會變化:
如果把依賴庫用默認(rèn)的方式打包為zip文件,那么sys.path[0]會輸出“C:\test\getpath\libarary.zip”;
如果在setup.py里面指定zipfile=None參數(shù),依賴庫就會被打包到exe文件里面,那么sys.path[0]會輸出“C:\test\getpath\getpath.exe”。
#!/bin/env python #-*- encoding=utf8 -*- import os,sys if __name__=="__main__": print "__file__=%s" % __file__ print "os.path.realpath(__file__)=%s" % os.path.realpath(__file__) print "os.path.dirname(os.path.realpath(__file__))=%s" % os.path.dirname(os.path.realpath(__file__)) print "os.path.split(os.path.realpath(__file__))=%s" % os.path.split(os.path.realpath(__file__))[0] print "os.path.abspath(__file__)=%s" % os.path.abspath(__file__) print "os.getcwd()=%s" % os.getcwd() print "sys.path[0]=%s" % sys.path[0] print "sys.argv[0]=%s" % sys.argv[0]
輸出結(jié)果:
D:\>python ./python_test/test_path.py __file__=./python_test/test_path.py os.path.realpath(__file__)=D:\python_test\test_path.py os.path.dirname(os.path.realpath(__file__))=D:\python_test os.path.split(os.path.realpath(__file__))=D:\python_test os.path.abspath(__file__)=D:\python_test\test_path.py os.getcwd()=D:\ sys.path[0]=D:\python_test sys.argv[0]=./python_test/test_path.py
os.getcwd() “D:\”,取的是起始執(zhí)行目錄
sys.path[0]或sys.argv[0] “D:\python_test”,取的是被初始執(zhí)行的腳本的所在目錄
os.path.split(os.path.realpath(__file__))[0] “D:\python_test”,取的是__file__所在文件test_path.py的所在目錄
正確獲取當(dāng)前的路徑:
__file__是當(dāng)前執(zhí)行的文件 # 獲取當(dāng)前文件__file__的路徑 print "os.path.realpath(__file__)=%s" % os.path.realpath(__file__) # 獲取當(dāng)前文件__file__的所在目錄 print "os.path.dirname(os.path.realpath(__file__))=%s" % os.path.dirname(os.path.realpath(__file__)) # 獲取當(dāng)前文件__file__的所在目錄 print "os.path.split(os.path.realpath(__file__))=%s" % os.path.split(os.path.realpath(__file__))[0]
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
利用python進(jìn)行矩陣運(yùn)算實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于如何利用python進(jìn)行矩陣運(yùn)算的相關(guān)資料,Numpy是Python編程語言中的一個(gè)核心庫,專門用于處理多維數(shù)據(jù)和矩陣運(yùn)算,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
Python Selenium中常用的元素定位方法總結(jié)
在Web自動化測試中,元素定位是一項(xiàng)非常重要的技術(shù),Python Selenium提供了各種元素定位方法,可以幫助我們定位頁面上的元素并與之交互,本文將詳細(xì)介紹Python Selenium中常用的元素定位方法,并提供實(shí)例代碼,需要的朋友可以參考下2023-11-11
Python 實(shí)現(xiàn)數(shù)據(jù)庫(SQL)更新腳本的生成方法
當(dāng)我們需要準(zhǔn)備更新腳本的使用,不小心會忘記改動了哪里,所以小編試著用Python來實(shí)現(xiàn)自動的生成更新腳本,具體操作方法,大家參考下本文吧2017-07-07
python實(shí)現(xiàn)查找兩個(gè)字符串中相同字符并輸出的方法
這篇文章主要介紹了python實(shí)現(xiàn)查找兩個(gè)字符串中相同字符并輸出的方法,涉及Python針對字符串操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Python 3.8中實(shí)現(xiàn)functools.cached_property功能
這篇文章主要介紹了Python 3.8中實(shí)現(xiàn)functools.cached_property功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Python利用QQ郵箱發(fā)送郵件的實(shí)現(xiàn)方法(分享)
下面小編就為大家?guī)硪黄狿ython利用QQ郵箱發(fā)送郵件的實(shí)現(xiàn)方法(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

