Roggan
Roggan

Reputation: 175

Python pathlib: How to filter files from path with expression matching?

I would like to filter out files containing a certain expression within a directory. I'm quite new to Python. In R I would have worked with Strings, but in Python I worked with pathlib, which has some neat advantages such as cross-plattform compatibility.

E.g I have a list made of WindowsPath objects to filter:

from pathlib import Path
import re, glob

fileDir = Path(r'T:\testdir')
filelist = [img for img in fileDir.iterdir() if img.is_file()]

print(filelist)
[WindowsPath('T:/testdir/T31TGL_20180108T104421_B11.jp2'), WindowsPath('T:/testdir/T31TGL_20180108T104421_B12.jp2'), WindowsPath('T:/testdir/T31TGL_20180108T104421_TCI.jp2')]

Desired output is the path to the file T31TGL_20180108T104421_TCI.jp2. E.g. I want to filter the files contained in filelist for the expression TCI.

I tried to filter using expression matching as follows:

expr = re.compile('.*TCI')
TCI_path = list(filter(expr.match, imglist))

THis throws the error:

TypeError: expected string or bytes-like object

e.g. does not work with the WindowsPath objects of the filelist. I also tried with the .glob method (https://docs.python.org/3.5/library/glob.html#glob.glob) but am too inexperienced with python to seem to get it to work...

How can I filter this list accordingly?

Upvotes: 2

Views: 8585

Answers (2)

Howard
Howard

Reputation: 1

Also you can use pathlib directly

from pathlib import Path
folderPath = Path('some/path/to/dir')

for file in folderPath.glob("*.txt"):
    print(file.name)

Upvotes: 0

hiro protagonist
hiro protagonist

Reputation: 46839

you can convert your WindowsPaths to str before matching:

TCI_path = list(item for item in filelist if expr.match(str(item)))

.glob("*TCI*") (or maybe even .glob("*_TCI.jp2")) should also work...

Upvotes: 4

Related Questions