How to control order of result from iterator in python

Viewed 8766

I use pathlib.Path().iterdir() to get sub-dictionary of the path.

Under /home/yuanyi/workspace/app, there are 4 folders: 01, 02, 03, 04.

from pathlib import Path
for subdir in Path('/home/yuanyi/workspace/app').iterdir():
    print(subdir)

But the result is not ordered.

/home/yuanyi/workspace/app/02
/home/yuanyi/workspace/app/03
/home/yuanyi/workspace/app/01
/home/yuanyi/workspace/app/00

Wht the result is not the following:

/home/yuanyi/workspace/app/01
/home/yuanyi/workspace/app/02
/home/yuanyi/workspace/app/03
/home/yuanyi/workspace/app/04

I want to know how the iterator works, and what's the best method to get ordered result.

2 Answers
Related