每天学习一个python的类(大多数都是第三方的),聚沙成金。
--------------------------------------------------------------------------------
今天学习的是:pathlib:(Python3.4+ 标准库)跨平台的、面向对象的路径操作库.
其官方网址为:https://pathlib.readthedocs.io/en/pep428/
如果只是把path作为string对象来操作,我们会碰到很多很繁琐的操作,因此,pathlib就是对os.path进行了封装,提供了一个便捷的,面向对象的操作方式
而且,从python3.4开始,这个类就是标准类库了
his module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations
几个常见的例子:
1 from pathlib import * 2 3 p=Path('.') 4 for x in p.iterdir(): 5 if x.is_dir(): 6 print(x)