nos
nos

Reputation: 20880

How to exclude hidden directories from Pathlib glob('**')

I use python pathlib Path.glob('**') to get the main directory and all the sub-directories recursively. But I don't want the hidden directories. My code looks like this

from pathlib import Path

p = Path(a_path)
p.glob('**')

What I want can be achieved from glob like this

glob.glob(os.path.join(a_path,'**/'), recursive=True)

But I am curious how to achieve the same effect with pathlib

Upvotes: 3

Views: 1428

Answers (1)

Jan Wilamowski
Jan Wilamowski

Reputation: 3598

The pathlib version of glob doesn't filter hidden files and directories so you'll have to check them yourself. Since it returns all entries, you have to check every part (path component) separately:

[path for path in p.glob('**') if not any(part.startswith('.') for part in path.parts)]

This has several problems: it's inefficient for large hidden directories because you can't remove them just once, you have to filter out every single of their subfolders. It also won't work if any part of your starting folder is hidden, unless you cut that off in the if check.

An alternative would be to use use os.walk() which allows you to modify the descent while it happens, as described in this answer.

This all assumes you're using *nix where the hidden status is indicated by adding a dot in front of the name. On Windows you might have to resort to external tools like attrib.

Upvotes: 3

Related Questions