Reputation: 966
I have a directory containing files named like so:
2018-07-14
2018-07-12
2018-07-17
Right now I am iterating over all those files like so:
from pathlib import Path
def data_generator(my_dir):
data_path = Path(my_dir)
for path in data_path.iterdir():
print(path)
Is there a simple to make sure I iterate on the files in order using their name as key, from oldest to most recent?
Upvotes: 0
Views: 443
Reputation: 2318
You can enclose data_path.iterdir()
with a sorted()
function.
from pathlib import Path
def data_generator(my_dir):
data_path = Path(my_dir)
for path in sorted(data_path.iterdir()):
print(path)
Upvotes: 3