Python学习笔记-os.path.join()学习笔记

os.path.join()用法以及代码示例

python中的OS模块提供了与操作系统进行交互的功能,操作系统属于python的标准实用程序模块。该模块提供了使用依赖与操作系统的功能的便携式方法。os.path模块是python中os模块的sub-module,用于通用路径名操作

os.path.join()是python中方法,可以智能地连接一个或者多个路径组件。此方法将各个路径组成部分与每个非空部分之后的最后一个路径组成部分恰好用一个目录分隔符进行连接,如果要链接的最后一个路径组件为空,则将目录分隔符/放在末尾

如果路径组件表示绝对路径,那么将放弃所有先前连接的组件,并且从绝对路径组件进行连接。

用法:os.path.join(path,*paths)

参数:

path:代表文件系统路径的path-like对象
*paths:代表文件系统路径的path-like对象,他表示要链接的路径组件
path-like对象是表示路径的字符串或者字符对象

Note:python函数定义中的特殊语法args(此处为paths 用于将可变数量的参数传递给函数)

返回类型:此方法返回一个表示串联路径组件的字符串
代码:使用os.path.join()方法连接各种路径组件

# Python program to explain os.path.join() method  
    
# importing os module  
import os 
  
# Path 
path = "/home"
  
# Join various path components  
print(os.path.join(path, "User/Desktop", "file.txt")) 
  
  
# Path 
path = "User/Documents"
  
# Join various path components  
print(os.path.join(path, "/home", "file.txt")) 
  
# In above example '/home' 
# represents an absolute path 
# so all previous components i.e User / Documents 
# are thrown away and joining continues  
# from the absolute path component i.e / home. 
  
  
# Path 
path = "/User"
  
# Join various path components  
print(os.path.join(path, "Downloads", "file.txt", "/home")) 
  
# In above example '/User' and '/home' 
# both represents an absolute path 
# but '/home' is the last value 
# so all previous components before '/home' 
# will be discarded and joining will 
# continue from '/home'  
  
# Path 
path = "/home"
  
# Join various path components  
print(os.path.join(path, "User/Public/", "Documents", "")) 
  
# In above example the last  
# path component is empty 
# so a directory seperator ('/') 
# will be put at the end 
# along with the concatenated value

输出:

/home/User/Desktop/file.txt
/home/file.txt
/home
/home/User/Public/Documents/
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Pythonos模块中的os.walk()函数是用来遍历文件夹的。它可以递归地遍历指定文件夹下的所有子文件夹和文件,并返回一个包含每个文件夹路径、子文件夹列表和文件列表的三元组的生成器。 而os.path.join()函数是用来连接路径的。它可以将多个路径组合成一个新的路径,并根据操作系统的不同自动添加正确的路径分隔符。这样可以方便地创建文件的绝对路径。 举个例子,如果我们有一个文件夹路径 `c:\Python`,并且文件夹中有一个名为 `a.txt` 的文件,我们可以使用os.path.join()函数将文件夹路径和文件名连接起来,生成完整的文件路径 `'c:\\Python\\a.txt'`。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python os.walk() 方法遍历文件目录](https://blog.csdn.net/weixin_34567079/article/details/114911951)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [如何用Python os.path.walk方法遍历搜索文件内容的操作详解_](https://blog.csdn.net/weixin_42216454/article/details/113963002)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值