【Python】如何使用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】如何在Python中进行数据聚合和分组
- 如何在Python中进行数据聚合和分组在数据分析和处理的过程中,经常需要对数据进行聚合和分组操作。Python提供了各种强大的库和工具,方便我们进行数据聚合和分组的操作。本文将介绍如何在Python中使用pandas库进行数据聚合和分组,并提供具体的代码示例。一、数据聚合数据聚合是将多个数据合并成一个或少量几个数据的操作。在Python中,可以使用pandas库中的groupby()函数进行数据聚合。示例代码如下:import pandas as pd
- 【Python】matplotlib显示中文字符的有效方法详解
- 详解matplotlib中显示中文的有效方法,需要具体代码示例在数据可视化中,matplotlib是一个非常常用的库,它提供了强大且灵活的绘图功能。然而,matplotlib默认不支持显示中文字符,这给使用者带来了不便。本文将介绍一些在matplotlib中显示中文的有效方法,并提供具体的代码示例。方法一:使用系统字体matplotlib可以通过设置系统字体路径来实现显示中文。首先,我们需要找到系统中对应的字体文件,比如微软雅黑字体的路径为"C:/Windows/Fonts/msyh.
- 【Python】pycharm如何安装Python
- 安装步骤:1、打开PyCharm并打开你的项目;2、转到"File">“Settings”;3、选择"Project">“Python Interpreter”;4、在右上角的设置窗口中,点击"+"符号添加新的解释器;5、选择"Existing interpreter";6、浏览并选择你系统中已经安装的Python解释器;7、点击"OK"即可。本教程操作系统:windows10系统、P
- 【Python】用matplotlib实现数据集散点图的实际应用
- 实战演练:利用Matplotlib绘制数据集的散点图Matplotlib是Python中常用的绘图库之一,它提供了丰富的功能,可以绘制各种类型的图表。其中,散点图是一种常用的数据可视化方式,用于展示两个变量之间的关系。本文将介绍如何利用Matplotlib绘制数据集的散点图,并附上具体的代码示例。首先,我们需要安装Matplotlib库。可以使用pip命令执行以下语句安装:pip install matplotlib安装完成后,我们可以导入Matplotlib库并开始绘制散点
- 【Python】10个Python代码分析工具,助力高效编程
- 文章目录前言10. cProfile和profile8. Pytest9. Coverage6. Black7. isort1. Pylint2. Flake83. MyPy4. Bandit5. Safety代码分析工具代码格式化工具测试工具性能分析工具总结Python入门全套学习资料附带源码:Python零基础入门视频Python项目源码Python入门到进阶电子书籍和实战案例👉100道Python练习题👈👉面试刷题👈资料领取
- 【Python】如何用Python编写SVM算法
- 如何用Python编写SVM算法?SVM(Support Vector Machine)是一种常用的分类和回归算法,基于统计学习理论和结构风险最小化原理。它具有较高的准确性和泛化能力,并且适用于各种数据类型。在本篇文章中,我们将详细介绍如何使用Python编写SVM算法,并提供具体的代码示例。安装Python和相关库在开始编写SVM算法之前,首先需要确保已经安装了Python和相关的机器学习库。推荐使用Anaconda作为Python的集成开发环境,它不仅自带了Python解释器,还包括了很多常
- 【Python】Python实现多继承的方法和关注点
- Python多继承的实现方法及注意事项多继承是Python中一个重要的特性,它允许一个类继承多个父类的属性和方法。在实际开发中,多继承可以帮助我们更好地组织和重用代码。本文将介绍Python中多继承的实现方法,并提供一些注意事项。一、多继承的基本概念多继承是指一个类可以同时继承多个父类的特性。在Python中,多继承是通过使用逗号分隔的多个父类来实现的。二、多继承的实现方法方法一:使用super()函数super()函数是一个内置函数,它可以调用父类的方法。在多继承的情况下,可以通过super(
- 【Python】深入研究matplotlib的色彩映射表
- 深入学习matplotlib颜色表,需要具体代码示例一、引言matplotlib是一个功能强大的Python绘图库,它提供了丰富的绘图函数和工具,可以用于创建各种类型的图表。而颜色表(color map)是matplotlib中一个重要的概念,它决定了图表的配色方案。深入学习matplotlib颜色表,将帮助我们更好地掌握matplotlib的绘图功能,使绘图结果更加美观和有序。本文将介绍颜色表的概念,并给出一些具体的代码示例,以帮助读者更好地理解和应用。二、什么是颜色表颜色表是一个颜色映射表,