Four Tips for Mastering Python’s pathlib Module

ifeelfree
2 min readOct 25, 2023
Path from unsplash

Tips 1: search for contents in a given folder

(1) list all the contents including subfolders

import pathlib 
target_dir='/home/feifei/go/'
cnt = 0
for f in pathlib.Path(target_dir).glob("**/*"):
print(f)
cnt = cnt + 1
if cnt == 5:
break

# /home/feifei/go/pkg
# /home/feifei/go/bin
# /home/feifei/go/pkg/sumdb
# /home/feifei/go/pkg/mod
#…

--

--