目录
算法(步骤)
从文件路径获取主文件名
示例
输出
从文件路径获取目录名
将路径组件连接在一起
os.path.join()函数
扩展用户的主目录
os.path.expanduser()函数
语法
从文件路径中分离文件扩展名
结论
首页 后端开发 Python教程 如何使用Python操作路径名?

如何使用Python操作路径名?

Sep 15, 2023 pm 07:13 PM
python路径操作 python路径处理 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操作路径名?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

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

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何解决Linux终端中查看Python版本时遇到的权限问题? 如何解决Linux终端中查看Python版本时遇到的权限问题? Apr 01, 2025 pm 05:09 PM

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

在Python中如何高效地将一个DataFrame的整列复制到另一个结构不同的DataFrame中? 在Python中如何高效地将一个DataFrame的整列复制到另一个结构不同的DataFrame中? Apr 01, 2025 pm 11:15 PM

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? 如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? Apr 02, 2025 am 07:18 AM

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

Uvicorn是如何在没有serve_forever()的情况下持续监听HTTP请求的? Uvicorn是如何在没有serve_forever()的情况下持续监听HTTP请求的? Apr 01, 2025 pm 10:51 PM

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

Python中如何通过字符串动态创建对象并调用其方法? Python中如何通过字符串动态创建对象并调用其方法? Apr 01, 2025 pm 11:18 PM

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...

哪些流行的Python库及其用途? 哪些流行的Python库及其用途? Mar 21, 2025 pm 06:46 PM

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? 如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? Apr 02, 2025 am 07:15 AM

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

See all articles