![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python标准库
李木子木子
这个作者很懒,什么都没留下…
展开
-
python 中的 __enter__ 和__exit__
为了支持with语句,可以在类中定义__enter__和__exit__函数 class ceshi(): def __init__(self): pass def __enter__(self): print('进入with语句') return self def __exit__(self,*args): print('退出with') with ceshi() as c: print(c) 输出: 进入原创 2022-04-19 16:00:17 · 1221 阅读 · 0 评论 -
python标准库(3.7版本)dir函数
dir函数带参数和不带参数作用域不一样 不带参数:返回当前范围内的变量、方法和定义的类型列表,如图: test_1 = dir() print(test_1) #输出 ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] import os test_2 = dir() print(test_2) #会输出定义的原创 2022-04-18 23:31:25 · 394 阅读 · 0 评论 -
python标准库(3.7版本)os.path
获得路径 os.path.abspath(path) #返回绝对路径(标准化的) os.path.basename(path) #返回该路径文件的名称。 os.path.commonpath(paths) #返回序列 paths 中每个路径名称的最长共同子路径。 如果 paths 同时包含绝对和相对路径名称或者如果 paths 为空则会引发 ValueError os.path.commonprefix(list) #接受包含多个路径的列表,返回所有路径的最长公共前缀(逐字符比较,结果不一定有效原创 2022-02-16 15:52:34 · 1036 阅读 · 0 评论