在本文中,我们将学习使用 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中文网其他相关文章!

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

Atom编辑器mac版下载
最流行的的开源编辑器

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)