(Nov-12-2022, 05:41 AM)tester_V Wrote: this one also looks ok but fails to print sorted files for each subdirectory.
.iterdir()
is not recursive so will not iterate over all subdirectory.Path.rglob wil recursiv iterate over all subdirectory.
Example and see that i only use
Pathlib
it has Path.stat() and no os module
import is needed.from pathlib import Path dest = Path(r'C:\Test') lst_files = [path for path in Path(dest).rglob('*') if path.is_file()] sort_files = sorted(lst_files, key=lambda t: t.stat().st_mtime, reverse=True) for file in sort_files: print(file)
Output:C:\Test\Ny mappe\farmer_tools.py
C:\Test\images\winter.jpg
C:\Test\test.txt
C:\Test\python
C:\Test\pip
Let say i modify winter.jpg image and save it,the it shoiuld be first as now sort bye modifaction time.Output:C:\Test\images\winter.jpg
C:\Test\Ny mappe\farmer_tools.py
C:\Test\test.txt
C:\Test\python
C:\Test\pip