如何使用Python操作路徑名稱?
在本文中,我們將學習使用 Python 操作路徑名稱。
以下是下面提到的一些不同的範例 -
從檔案路徑取得主檔案名稱
從檔案路徑取得目錄名稱
將路徑元件連接在一起
#擴充使用者的主目錄
#從檔案路徑分離檔案副檔名
演算法(步驟)
以下是執行所需任務所需遵循的演算法/步驟。 -
使用 import 關鍵字導入 os 模組。
建立一個變數來儲存輸入檔案路徑。
#使用os模組的basename()函數(傳回給定檔案路徑的基本名稱)來取得輸入檔案路徑的最後一個組成部分(主檔案名稱)並列印出來。
從檔案路徑取得主檔案名稱
範例
以下程式使用 os.path.basename() 函數從輸入檔案傳回主檔案名稱 -
# importing os module import os # input path of the file inputFilepath = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf' # Printing the given input path print("Give Input Path is:",inputFilepath) # getting the last component(main file name )of the input file path print("Base Name of the given path is :",os.path.basename(inputFilepath))
輸出
執行時,上述程式將產生以下輸出 -
Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf Base Name of the given path is: tutorialsPoint.pdf
從檔案路徑取得目錄名稱
使用os.path.dirname()函數(從給定檔案路徑返回目錄名稱)透過將輸入檔案路徑傳遞為來取得給定輸入檔案路徑的目錄/資料夾一個論點。
範例
以下程式使用 os.path.dirname() 函數從輸入檔案路徑返回目錄名稱 -
# importing os module import os # input path of the file inputFilepath = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf' # Printing the given input path print("Give Input Path is:",inputFilepath) # getting the directory/folder path from the input file path using dirname() function print("Directory path of the given path is: ",os.path.dirname(inputFilepath))
輸出
執行時,上述程式將產生以下輸出 -
Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf Directory path of the given path is: C:/Users/cirus/Desktop
將路徑元件連接在一起
os.path.join()函數
Python 的 os.path.join() 函數有效地連接一個或多個路徑元件。除了最後一個路徑元件之外,此方法透過在每個非空部分之後放置一個目錄分隔符號 ('/') 來連接不同的路徑元件。最後一個要加入的路徑元件為空時,在末尾加上目錄分隔符號(“/”)。
如果路徑元件表示絕對路徑,則所有先前連接的元件都將被刪除,並且連接將從絕對路徑元件開始繼續。
範例
以下程式使用 os.path.join() 函數將給定的路徑元件與基本名稱連接起來 -
# importing os module import os # input path of the file inputFilepath = 'C:/Users/cirus/Desktop/kohli.pdf' # Printing the given input path print("Give Input Path is:",inputFilepath) # joining the components to the main file name of the input file path print("Joining the given paths to input Path:\n", os.path.join('tutorials', 'python', os.path.basename(inputFilepath)))
輸出
執行時,上述程式將產生以下輸出 -
Give Input Path is: C:/Users/cirus/Desktop/kohli.pdf Joining the given paths to input Path: tutorials/python/kohli.pdf
擴充使用者的主目錄
os.path.expanduser()函數
Python 函數 os.path.expanduser() 將指定路徑中的初始路徑 ~(波形符號)或 ~user 擴充至使用者的主目錄。
文法
以下是該函數的語法。
os.path.expanduser(path)
範例
以下程式使用expanduser()函數傳回使用者主目錄的擴充路徑 -
# importing os module import os # input path of the file inputFilepath = '~/Users/cirus' # Printing the given input path print("Give Input Path is:",inputFilepath) # Expanding the user's home directory print("Expanded Path is:\n",os.path.expanduser(inputFilepath))
輸出
執行時,上述程式將產生以下輸出 -
Give Input Path is: ~/Users/cirus Expanded Path is: /root/Users/cirus
從檔案路徑分離檔案副檔名
os.path.splitext() 函數 - 將檔案路徑名拆分為一對根目錄和副檔名。這裡的根是除檔案副檔名之外的所有內容。
如果給定的檔案路徑沒有副檔名,則副檔名為空。如果給定路徑有前導句點(“.”),則路徑將被忽略。
文法
以下是該函數的語法。
os.path.splitext(path)
使用os.path.splitext()函數從輸入檔案路徑分割檔案路徑和檔案副檔名。
範例
以下程式使用 os.path.splitext() 函數從輸入檔案路徑分割主檔案路徑和檔案副檔名 -
# importing os module import os # input path of the file inputFilepath ='C:/Users/cirus/Desktop/tutorialsPoint.pdf' # Printing the given input path print("Give Input Path is:",inputFilepath) # splitting the file path and file extension from the input file path # using the splitext() function print("Splitting the given path by extension:\n",os.path.splitext(inputFilepath))
輸出
執行時,上述程式將產生以下輸出 -
Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf Splitting the given path by extension: ('C:/Users/cirus/Desktop/tutorialsPoint', '.pdf')
結論
在本文中,我們學習如何使用 OS 模組來修改路徑名。從檔案路徑中,我們學習如何提取主(基本)檔案名稱和目錄名稱。我們學習如何組合路徑的元件名稱和路徑。討論了使用者主目錄擴充過程。最後,我們找到瞭如何將檔案路徑與副檔名分開。
以上是如何使用Python操作路徑名稱?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

攻克Investing.com的反爬蟲策略許多人嘗試爬取Investing.com(https://cn.investing.com/news/latest-news)的新聞數據時,常常�...
