首頁  >  文章  >  後端開發  >  如何使用Python操作路徑名稱?

如何使用Python操作路徑名稱?

王林
王林轉載
2023-09-15 19:13:021034瀏覽

如何使用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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除