Python Forum
Failing to print sorted files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failing to print sorted files
#4
(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
tester_V likes this post
Reply


Messages In This Thread
Failing to print sorted files - by tester_V - Nov-12-2022, 04:05 AM
RE: Failing to print sorted files - by tester_V - Nov-12-2022, 04:16 AM
RE: Failing to print sorted files - by tester_V - Nov-12-2022, 05:41 AM
RE: Failing to print sorted files - by snippsat - Nov-12-2022, 10:38 AM
RE: Failing to print sorted files - by tester_V - Nov-12-2022, 06:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to connect by 'net use' tester_V 1 253 Apr-20-2024, 06:31 AM
Last Post: tester_V
  python print all files which contain specific word in it mg24 5 1,326 Jan-27-2023, 11:20 AM
Last Post: snippsat
  Failing regex tester_V 3 1,244 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  failing to print not matched lines from second file tester_V 14 6,263 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  set and sorted, not working how expected! wtr 2 1,323 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  Failing to connect to a host with WMI tester_V 6 4,496 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Failing to get Stat for a Directory tester_V 11 3,667 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Failing to Zip files tester_V 4 2,224 Dec-01-2020, 07:28 AM
Last Post: tester_V
  How to make elements return sorted? notsoexperienced 4 3,293 Sep-24-2020, 09:00 AM
Last Post: perfringo
  Why is my original list also sorted? Pedroski55 1 1,651 Jul-15-2020, 09:25 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020