当前位置: 移动技术网 > IT编程>脚本编程>Python > Python3中pathlib

Python3中pathlib

2018年12月26日  | 移动技术网IT编程  | 我要评论

郭景云,3d高清电影在线观看,华为在美国禁售

使用 pathlib 更好地处理路径

pathlib 是 python 3 的默认模块,帮助避免使用大量的 os.path.join()。

from pathlib import path

dataset = 'wiki_images'
datasets_root = path('/path/to/datasets/')

train_path = datasets_root / dataset / 'train'
test_path = datasets_root / dataset / 'test'

for image_path in train_path.iterdir():
    with image_path.open() as f: # note, open is a method of path object
        # do something with an image

  

拼接操作符:/

path对象 / path对象

path对象 / 字符串

字符串 / path对象

分解

parts属性,可以返回路径中的每一部分

joinpath

joinpath(*other)连接多个字符串到path对象中

其他方法

p.exists()
p.is_dir()
p.parts
p.with_name('sibling.png') # only change the name, but keep the folder
p.with_suffix('.jpg') # only change the extension, but keep the folder and the name
p.chmod(mode)
p.rmdir()

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网