我想找到所有图像并尝试使用pathlib,但我的reg表达式不起作用。我哪里出错了?
from pathlib import Path
FILE_PATHS=list(Path('./photos/test').rglob('*.(jpe?g|png)'))
print(len(FILE_PATHS))
FILE_PATHS=list(Path('./photos/test').rglob('*.jpg'))#11104
print(len(FILE_PATHS))
0
11104
使用正则表达式获取文件列表
import re
p = Path('C:/Users/user/Pictures')
files = []
for x in p.iterdir():
a = re.search('.*(jpe?g|png)',str(x))
if a is not None:
files.append(a.group())
使用
pathlib.Path
获取文件列表 then 使用正则表达式过滤列表 re
import re
from pathlib import Path
basepath = Path('C:/Users/user/Pictures')
pattern = '.*(jpe?g|png)'
matching_files = []
for _path in [p for p in basepath.rglob('*.*')]:
if re.match(pattern, _path.name):
matching_files.append(_path)
可以是这种形式。
from pathlib import Path
basepath = Path('C:/Users/user/Pictures')
pattern = '.*(jpe?g|png)'
matching_files = list(basepath.rglob(pattern))