如何使用 Python 操作路徑名?


在本文中,我們將學習如何使用 Python 操作路徑名。

下面是一些不同的示例 −

  • 從檔案路徑獲取主檔名

  • 從檔案路徑獲取目錄名

  • 將路徑元件組合在一起

  • 擴充套件使用者的 home 目錄

  • 從檔案路徑拆分副檔名

演算法(步驟)

以下是執行所需任務的演算法/步驟。−

  • 使用 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

擴充套件使用者的 home 目錄

os.path.expanduser() 函式

Python 函式 os.path.expanduser() 將指定路徑中的初始路徑 ~(波浪號) 或 ~user 擴充套件到使用者的 home 目錄。

語法

以下是函式的語法。

os.path.expanduser(path)

示例

以下程式使用 expanduser() 函式返回使用者 home 目錄的擴充套件路徑 −

# 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 模組修改路徑名。從檔案路徑中,我們學習瞭如何提取主要(基本)檔名和目錄名。我們學習瞭如何組合路徑的元件名稱和路徑。討論了使用者 home 目錄擴充套件過程。最後,我們弄清楚瞭如何將檔案路徑與副檔名分離。

更新於: 2023年1月27日

2K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.