python如何獲取當(dāng)前文件路徑

這篇文章主要介紹了python如何獲取當(dāng)前文件路徑,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)建站客戶idc服務(wù)中心,提供服務(wù)器托管、成都服務(wù)器、成都主機(jī)托管、成都雙線服務(wù)器等業(yè)務(wù)的一站式服務(wù)。通過(guò)各地的服務(wù)中心,我們向成都用戶提供優(yōu)質(zhì)廉價(jià)的產(chǎn)品以及開(kāi)放、透明、穩(wěn)定、高性價(jià)比的服務(wù),資深網(wǎng)絡(luò)工程師在機(jī)房提供7*24小時(shí)標(biāo)準(zhǔn)級(jí)技術(shù)保障。

Python獲取當(dāng)前路徑的方法:

import os,sys

使用sys.path[0]、sys.argv[0]、os.getcwd()、os.path.abspath(__file__)、os.path.realpath(__file__)

sys.path是Python會(huì)去尋找模塊的搜索路徑列表,sys.path[0]和sys.argv[0]是一回事因?yàn)镻ython會(huì)自動(dòng)把sys.argv[0]加入sys.path。

如果你在C:\test目錄下執(zhí)行python getpath\getpath.py,那么os.getcwd()會(huì)輸出“C:\test”,sys.path[0]會(huì)輸出“C:\test\getpath”。

如果你用py2exe模塊把Python腳本編譯為可執(zhí)行文件,那么sys.path[0]的輸出還會(huì)變化:
如果把依賴庫(kù)用默認(rèn)的方式打包為zip文件,那么sys.path[0]會(huì)輸出“C:\test\getpath\libarary.zip”;
如果在setup.py里面指定zipfile=None參數(shù),依賴庫(kù)就會(huì)被打包到exe文件里面,那么sys.path[0]會(huì)輸出“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] 

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享python如何獲取當(dāng)前文件路徑內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問(wèn)題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!

分享文章:python如何獲取當(dāng)前文件路徑
URL地址:http://www.muchs.cn/article34/ihpspe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、網(wǎng)站策劃、企業(yè)網(wǎng)站制作虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)