最近在做图像相关的算法,经常有对文件路径的一些条件遍历操作,发现最好使的还是下面这行神奇代码。
from pathlib import Path
imgs = [str(x) for x in Path('./dataset/images/').rglob('*.jp*g') if 'checkpoint' not in str(x)]
这行代码主要使用的是pathlib库,相比于传统的os模块的函数式文件操作,使用pathlib模块来操作文件系统更加简单,可读性更强。
pathlib库的核心类是pathlib.Path,所有功能都可以通过调用这个类的属性和方法进行实现,非常的面向对象。
公众号算法美食屋后台回复关键词:pathlib,获取本文jupyter notebook 源代码
一,Path构造
from pathlib import Path
#指定路径
mypath = Path('../demo.py')
print(mypath)
../demo.py
#当前路径
cwd = Path.cwd()
print(str(cwd))
/Users/liangyun2/CodeFiles/machine_learning_AK47/1_Feature_Engineering
#home路径
home = Path.home()
print(str(home))
/Users/liangyun2
二,Path属性
mypath = Path.cwd()
mypath.name #文件名
'1_Feature_Engineering'
mypath.suffix #后缀
''
mypath.parent #
PosixPath('/Users/liangyun2/CodeFiles/machine_learning_AK47')
list(mypath.parents)
[PosixPath('/Users/liangyun2/CodeFiles/machine_learning_AK47'),
PosixPath('/Users/liangyun2/CodeFiles'),
PosixPath('/Users/liangyun2'),
PosixPath('/Users'),
PosixPath('/')]
三,Path遍历
mypath = Path.cwd().parent
file = mypath/'1_Feature_Engineering'/'demo.py'
print(file)
/Users/liangyun2/CodeFiles/machine_learning_AK47/1_Feature_Engineering/demo.py
file.exists()
True
for sub in mypath.iterdir():
print(sub)
/Users/liangyun2/CodeFiles/machine_learning_AK47/.DS_Store
/Users/liangyun2/CodeFiles/machine_learning_AK47/3_Tune_Tools
/Users/liangyun2/CodeFiles/machine_learning_AK47/6_CV
/Users/liangyun2/CodeFiles/machine_learning_AK47/8_RL
/Users/liangyun2/CodeFiles/machine_learning_AK47/README.ipynb
/Users/liangyun2/CodeFiles/machine_learning_AK47/utils
/Users/liangyun2/CodeFiles/machine_learning_AK47/models
/Users/liangyun2/CodeFiles/machine_learning_AK47/2_Base_Models
/Users/liangyun2/CodeFiles/machine_learning_AK47/push_to_github.ipynb
/Users/liangyun2/CodeFiles/machine_learning_AK47/.gitignore
/Users/liangyun2/CodeFiles/machine_learning_AK47/.ipynb_checkpoints
/Users/liangyun2/CodeFiles/machine_learning_AK47/1_Feature_Engineering
/Users/liangyun2/CodeFiles/machine_learning_AK47/.git
/Users/liangyun2/CodeFiles/machine_learning_AK47/data
/Users/liangyun2/CodeFiles/machine_learning_AK47/7_NLP
/Users/liangyun2/CodeFiles/machine_learning_AK47/5_Integrated_Examples
/Users/liangyun2/CodeFiles/machine_learning_AK47/4_Ensemble_Tools
for sub in mypath.glob('*'):
print(sub)
/Users/liangyun2/CodeFiles/machine_learning_AK47/.DS_Store
/Users/liangyun2/CodeFiles/machine_learning_AK47/3_Tune_Tools
/Users/liangyun2/CodeFiles/machine_learning_AK47/6_CV
/Users/liangyun2/CodeFiles/machine_learning_AK47/8_RL
/Users/liangyun2/CodeFiles/machine_learning_AK47/README.ipynb
/Users/liangyun2/CodeFiles/machine_learning_AK47/utils
/Users/liangyun2/CodeFiles/machine_learning_AK47/models
/Users/liangyun2/CodeFiles/machine_learning_AK47/2_Base_Models
/Users/liangyun2/CodeFiles/machine_learning_AK47/push_to_github.ipynb
/Users/liangyun2/CodeFiles/machine_learning_AK47/.gitignore
/Users/liangyun2/CodeFiles/machine_learning_AK47/.ipynb_checkpoints
/Users/liangyun2/CodeFiles/machine_learning_AK47/1_Feature_Engineering
/Users/liangyun2/CodeFiles/machine_learning_AK47/.git
/Users/liangyun2/CodeFiles/machine_learning_AK47/data
/Users/liangyun2/CodeFiles/machine_learning_AK47/7_NLP
/Users/liangyun2/CodeFiles/machine_learning_AK47/5_Integrated_Examples
/Users/liangyun2/CodeFiles/machine_learning_AK47/4_Ensemble_Tools
for sub in mypath.glob('*.ipynb'):
print(sub)
/Users/liangyun2/CodeFiles/machine_learning_AK47/README.ipynb
/Users/liangyun2/CodeFiles/machine_learning_AK47/push_to_github.ipynb
files = [x for x in mypath.rglob('*') if x.is_file()]
len(files)
9670
四,Path操作
path = Path.cwd()/'test.txt'
if not path.exists():
path.touch() #创建文件
#读写内容
path.write_text("hello world!\n你好,中国!\n")
path.read_text()
'hello world!\n你好,中国!\n'
#增量形式写
with path.open('a') as f:
f.write('你好,北京')
path.read_text()
'hello world!\n你好,中国!\n你好,北京'
#重命名
path = path.rename('hello.txt')
# %load hello.txt
hello world!
你好,中国!
你好,北京
公众号算法美食屋后台回复关键词:pathlib,获取本文jupyter notebook 源代码。