<浅>介绍-path库


前言

最近在看代码的时候碰到了path库,代码中经常使用from path import Path来导入对路径进行管理,起初认为是pathlib,因为代码中很多路径拼接的时候是直接使用/进行拼接的,而os.path则不是这样,但是在其他文件中确出现了os.path相关的操作,比如.realpath()或者是.dirname(),这就让我很不解,而且百度谷歌中都没有查到相关的词条。在查找的时候突然想起来在配置环境的时候,有使用pip安装过这个包pip install path,所以便去Python官方的网站去找,之后恍然大悟,感叹自己之前的傻,浪费了很多时间。


第三方库-path库

在Python中,安装第三方模块,是通过包管理工具pip完成的。一般来说,第三方库都会在Python官方的pypi.python.org网站注册,可以在官网或者pypi上搜索相关的库名进行查阅这里也只是做一个简单的介绍。

1、简介

原文:path (aka path pie, formerly path.py) implements path objects as first-class entities, allowing common operations on files to be invoked on those path objects directly. The path.py project was initially released in 2003 by Jason Orendorff and has been continuously developed and supported by several maintainers over the years.

译文:path.py项目最初由 Jason Orendorff 于 2003 年发布,多年来一直得到多个维护者的持续开发和支持。
path将路径对象实现为第一类实体,允许直接在这些路径对象上调用文件的常见操作。

很明显这里的path不是pathlib也不是os.path,但是它又和它们有着很多的联系

2、优点

Python 3.4 引入了 pathlib,它与path有许多共同的特点。特别是,它提供了用于表示文件系统路径的对象封装。但是实现和使用很快就出现了分歧,并且pathpathlib 有几个优点:引自官网

  1. pathPath对象实现为 str的子类,因此这些Path 对象可以直接传递给其他期望简单的路径文本表示的 API,而使用pathlib,必须首先将值转换为字符串,然后再将它们传递给不知道的 API路径库。PEP519在 Python3.6中解决了这个缺点。
  2. path不仅暴露了路径的基本功能,还暴露了路径上的常用行为,提供了诸如rmtree(来自 shlib)和remove_p(如果文件存在则删除文件)之类的方法。
  3. 作为 PyPI 托管的包,path可以比 stdlib 包更快地自由迭代。
  4. path为其 Path 对象提供了统一的抽象,从而使实现者可以轻松地对其进行子类化。不能子类化 pathlib.Path以添加功能,但必须子类化PathPosixPathWindowsPath,即使只想将_ _dict_ _添加到子类实例。path相反允许Path.module 对象被子类覆盖,默认为os.path。即使是对模型进行子类化的path.Path的高级使用,也不需要关注特定于操作系统的细微差别。
  5. 这个路径项目的明确目标是尽可能提供与pathlib对象的兼容性,例如path.Path 对象是pathlib.Path*对象的替代品。

3、实例

这里我不做过多的举例,因为实在是很多,浅写几个例子,如果想要了解全部的话,可以通过链接去查看相关的API ,如果之前有了解过pathlibos.path的话,那么看这些API的时候应该会很轻松。

>>> from path import Path #导入包
>>> d=Path('/home/king/kki')  #随机写的路径

>>> d
>>> Path('/home/king/kki')

>>> type(d) #d的类型
>>> <class 'path.Path'>

>>> k=d/'001.txt' #路径拼接
>>> k
>>> Path('/home/king/kki/001.txt')

>>> c=sorted(d.files('*.txt')) #使用sorted默认排序,返回新的list
>>> c
>>> [Path('/home/king/kki/010.txt'), Path('/home/king/kki/458.txt'), Path('/home/king/kki/562.txt'), Path('/home/king/kki/752.txt')]

>>> c[0] #索引查询
>>> Path('/home/king/kki/010.txt')

>>> a=[c[0]/folder[:-1] for folder in open(c[0])]
>>> [Path('/home/king/kki/010.txt/0123546987520'), Path('/home/king/kki/010.txt/0123658647825'), Path('/home/king/kki/010.txt/1256397452126'), Path('/home/king/kki/010.txt/2663985478529')]

>>> c[0].dirname() #目录名
>>> Path('/home/king/kki')
.......

小结

知识匮乏使我麻麻的,有不准确的地方欢迎批评指正,推荐大家去看官网原文的介绍,具体方法具体了解。谢谢!!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<form class="ant-form ant-form-horizontal"><div class="ant-row ant-form-item"style="row-gap: 0px;"><div class="ant-col ant-form-item-label"style="width: 100px;"><label for="form_item_licDetailType"class="ant-form-item-required"title="license类型">license类型<!----></label></div><div class="ant-col ant-form-item-control"><div class="ant-form-item-control-input"><div class="ant-form-item-control-input-content"><div class="ant-select ant-select-single ant-select-allow-clear ant-select-show-arrow"><!----><div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search"id="form_item_licDetailType"autocomplete="off"class="ant-select-selection-search-input"role="combobox"aria-haspopup="listbox"aria-owns="form_item_licDetailType_list"aria-autocomplete="list"aria-controls="form_item_licDetailType_list"aria-activedescendant="form_item_licDetailType_list_0"readonly=""unselectable="on"style="opacity: 0;"aria-expanded="false"></span><!----><span class="ant-select-selection-placeholder">请选择</span></div><span class="ant-select-arrow"unselectable="on"aria-hidden="true"style="user-select: none;"><span role="img"aria-label="down"class="anticon anticon-down ant-select-suffix"><svg focusable="false"class=""data-icon="down"width="1em"height="1em"fill="currentColor"aria-hidden="true"viewBox="64 64 896 896"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></span><!----></div></div><!----></div><!----><!----></div></div></form> 请进行selemiu 元素定位
07-13

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值