The goal of this series of videos is to demonstrate how to deal with files, paths and folders from python programmatically. We'll mainly discuss the python pathlib module.
You can iterate over files and folders that match a pattern
by using the
.glob() method on a path. An example is listed below;
[p for p in Path().glob("data/*/*.json")]
Note the power of having the list comprehension. Every
p in the
loop below is a
Path-object. This means that we can apply all
methods on it that we mentioned before. Here's an example;
[p.parent for p in Path().glob("data/*/*.json")]
But you can do anything you'd like here. For example, maybe
we also would like to allow for
[p for p in Path().glob("data/*/*") if p.suffix in ('.csv', '.json')]
Comprehensions are powerful, also with
Path.glob(). There's even
a more powerful setting that you can do by using
** instead of
[p for p in Path().glob("**/*.json")]
The results will now be found recursively in all folders.
Feedback? See an issue? Something unclear? Feel free to mention it here.
If you want to be kept up to date, consider signing up for the newsletter.