python每日一类(1):pathlib

每天学习一个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)
View Code

 显示某个路径下的py的源文件

 list(p.glob('*.py'))


其源代码的引入的类如下;
import fnmatch #fnmatch 模块使用模式来匹配文件名.
import functools 用于高阶函数:指那些作用于函数或者返回其它函数的函数,通常只要是可以被当做函数调用的对象就是这个模块的目标。
import io
import ntpath
import os
import posixpath
import re
import sys
import time
from collections import Sequence
from contextlib import contextmanager
from errno import EINVAL, ENOENT
from operator import attrgetter
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
try:
    from urllib import quote as urlquote, quote as urlquote_from_bytes
except ImportError:
    from urllib.parse import quote as urlquote, quote_from_bytes as urlquote_from_bytes

      

原文地址:https://www.cnblogs.com/aomi/p/6919965.html