以下是下面提到的一些不同的示例 -
从文件路径获取主文件名
从文件路径获取目录名
将路径组件连接在一起
扩展用户的主目录
从文件路径中分离文件扩展名
算法(步骤)以下是执行所需任务所需遵循的算法/步骤。 -
使用 import 关键字导入 os 模块。
创建一个变量来存储输入文件路径。
使用os模块的basename()函数(返回给定文件路径的基本名称)来获取输入文件路径的最后一个组成部分(主文件名)并打印出来。
从文件路径获取主文件名示例以下程序使用 os.path.basename() 函数从输入文件返回主文件名 -
# importing os moduleimport os # input path of the file inputfilepath = 'c:/users/cirus/desktop/tutorialspoint.pdf'# printing the given input pathprint(give input path is:,inputfilepath)# getting the last component(main file name )of the input file pathprint(base name of the given path is :,os.path.basename(inputfilepath))
输出执行时,上述程序将生成以下输出 -
give input path is: c:/users/cirus/desktop/tutorialspoint.pdfbase name of the given path is: tutorialspoint.pdf
从文件路径获取目录名使用os.path.dirname()函数(从给定文件路径返回目录名)通过将输入文件路径传递为来获取给定输入文件路径的目录/文件夹一个论据。
示例以下程序使用 os.path.dirname() 函数从输入文件路径返回目录名称 -
# importing os moduleimport os # input path of the file inputfilepath = 'c:/users/cirus/desktop/tutorialspoint.pdf'# printing the given input pathprint(give input path is:,inputfilepath)# getting the directory/folder path from the input file path using dirname() functionprint(directory path of the given path is: ,os.path.dirname(inputfilepath))
输出执行时,上述程序将生成以下输出 -
give input path is: c:/users/cirus/desktop/tutorialspoint.pdfdirectory path of the given path is: c:/users/cirus/desktop
将路径组件连接在一起os.path.join()函数python 的 os.path.join() 函数有效地连接一个或多个路径组件。除了最后一个路径组件之外,此方法通过在每个非空部分之后放置一个目录分隔符 ('/') 来连接不同的路径组件。最后一个要加入的路径组件为空时,在末尾添加目录分隔符(“/”)。
如果路径组件表示绝对路径,则所有先前连接的组件都将被删除,并且连接将从绝对路径组件开始继续。
示例以下程序使用 os.path.join() 函数将给定的路径组件与基本名称连接起来 -
# importing os moduleimport os # input path of the file inputfilepath = 'c:/users/cirus/desktop/kohli.pdf'# printing the given input pathprint(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.pdfjoining 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 moduleimport os# input path of the file inputfilepath = '~/users/cirus'# printing the given input pathprint(give input path is:,inputfilepath)# expanding the user's home directoryprint(expanded path is:\n,os.path.expanduser(inputfilepath))
输出执行时,上述程序将生成以下输出 -
give input path is: ~/users/cirusexpanded path is: /root/users/cirus
从文件路径中分离文件扩展名os.path.splitext() 函数 - 将文件路径名拆分为一对根目录和扩展名。这里的根是除文件扩展名之外的所有内容。
如果给定的文件路径没有扩展名,则扩展名为空。如果给定路径有前导句点(“.”),则该路径将被忽略。
语法以下是该函数的语法。
os.path.splitext(path)
使用os.path.splitext()函数从输入文件路径中分割文件路径和文件扩展名。
示例以下程序使用 os.path.splitext() 函数从输入文件路径中分割主文件路径和文件扩展名 -
# importing os moduleimport os# input path of the file inputfilepath ='c:/users/cirus/desktop/tutorialspoint.pdf'# printing the given input pathprint(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.pdfsplitting the given path by extension: ('c:/users/cirus/desktop/tutorialspoint', '.pdf')
结论在本文中,我们学习了如何使用 os 模块来修改路径名。从文件路径中,我们学习了如何提取主(基本)文件名和目录名。我们学习了如何组合路径的组件名称和路径。讨论了用户主目录扩展过程。最后,我们找到了如何将文件路径与扩展名分开。
以上就是如何使用python操作路径名?的详细内容。
