溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python中的路徑拼接問題怎么解決

發(fā)布時(shí)間:2023-03-09 14:43:04 來源:億速云 閱讀:120 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“python中的路徑拼接問題怎么解決”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

    python路徑拼接

    使用:

    • os.path.join()函數(shù):連接兩個(gè)或更多的路徑名組件

    • 如果有一個(gè)組件是一個(gè)絕對(duì)路徑,則在它之前的所有組件均會(huì)被舍棄

    • 如果最后一個(gè)組件為空,則生成的路徑以一個(gè) \ 分隔符結(jié)尾

    def test2():
        s1, s2, s3 = 'home', 'courses', 'test'
        res = os.path.join(s1, s2, s3)
        print(res)  # home\courses\test
        s2 = '/courses'
        res = os.path.join(s1, s2, s3)
        print(res)  # /courses\test
        s1, s2, s3 = '\home', 'courses', 'test'
        res = os.path.join(s1, s2, s3)
        print(res)  # \home\courses\test
        s1, s2, s3 = '\home', 'courses', ''
        res = os.path.join(s1, s2, s3)
        print(res)  #\home\courses\

    home\courses\test
    /courses\test
    \home\courses\test
    \home\courses\

    python os.path.join路徑拼接錯(cuò)誤

    ss = 'E:\\Cloud\\20200813105812L\\res\\1\\425'
    a = '\\8_live_1962854245_export_files\\media'
    
    c = os.path.join(ss,a)
    print(c)

    得到的是如下結(jié)果:

    python中的路徑拼接問題怎么解決

    os.path.join()函數(shù)

    連接兩個(gè)或更多的路徑名組件:

    參數(shù)可能存在多個(gè)

    從右邊開始數(shù),遇到第一個(gè)以”/”開頭的參數(shù),開始拼接,這個(gè)參數(shù)左邊的全部丟棄

    print("0:",os.path.join('\\aaaa','bbbb','ccccc.txt'))
    print("0:",os.path.join('aaaa','\\bbbb','ccccc.txt'))
    print("0:",os.path.join('aaaa','bbbb','\\ccccc.txt'))
    print("0:",os.path.join('aaaa','\\bbbb','\\ccccc.txt'))
    print("0:",os.path.join('aaaa','/bbbb','\\ccccc.txt'))
    print("0:",os.path.join('aaaa','/bbbb','/ccccc.txt'))
    
    結(jié)果:
    0: \aaaa\bbbb\ccccc.txt
    0: \bbbb\ccccc.txt
    0: \ccccc.txt
    0: \ccccc.txt	
    0: \ccccc.txt
    0: /ccccc.txt

    從右邊開始數(shù),遇到第一個(gè)以”/”開頭的參數(shù),開始拼接,這個(gè)參數(shù)左邊的全部丟棄

    print("1:",os.path.join('aaaa','xxxxxx','./bbb','ccccc.txt'))
    print("1:",os.path.join('./aaaa','xxxxxx','./bbb','./ccccc.txt'))
    
    結(jié)果:
    1: aaaa\xxxxxx\./bbb\ccccc.txt
    1: ./aaaa\xxxxxx\./bbb\./ccccc.txt

    帶盤符,/,\各種復(fù)雜情況(正式使用前先測(cè)試,也可以去看看源碼具體是怎么解析的)

    # 后面的斜杠反斜杠
    print("1:",os.path.join('c:','bbb'))
    print("1:",os.path.join('c:','bbb/\\','ccccc.txt'))
    
    # 未加盤符
    print("2:",os.path.join('c','/bbb','ccccc.txt')) # 會(huì)以/bbb開頭
    print("2:",os.path.join('c:','/bbb','ccccc.txt')) # 雖然有反斜桿 但是依舊以C:開頭
    print("2:",os.path.join('c:/','/bbb','ccccc.txt')) # 多個(gè)/只會(huì)有一個(gè)
    
    # 盤符后面未加斜杠
    print("3:",os.path.join('c:','bbb','ccccc.txt'))
    print("3:",os.path.join('c:/','bbb','ccccc.txt'))
    
    結(jié)果:
    1: c:bbb
    1: c:bbb/\ccccc.txt
    2: /bbb\ccccc.txt
    2: c:/bbb\ccccc.txt
    2: c:/bbb\ccccc.txt
    3: c:bbb\ccccc.txt
    3: c:/bbb\ccccc.txt

    “python中的路徑拼接問題怎么解決”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI