在本文中,我們將學習使用 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中文網其他相關文章!

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

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

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。