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() 将指定路径中的初始路径~(波浪线符号)或~用户扩展为用户的主目录。
语法
以下是该函数的语法。
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模块来修改路径名。从文件路径中,我们学习了如何提取主文件名和目录名。我们学会了如何组合路径的组件名称和路径。我们还讨论了用户主目录的扩展过程。最后,我们找出了如何从文件路径中分离出扩展名。