Python Forum
Failing to print sorted files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failing to print sorted files
#1
Greetings!
I'm trying to sort files in a directory by the timestamp.
I got this snipped from the internet and if I specify the directory the snipped works but if I supply the directory by "iterdir()" it fails to print sorted files.
dir_name = 'c:/01/'
list_of_files = filter( os.path.isfile,
                        glob.glob(dir_name + '*') )
list_of_files = sorted( list_of_files,
                        key = os.path.getmtime)
for file_path in list_of_files:
    timestamp_str = time.strftime(  '%m/%d/%Y :: %H:%M:%S',
                                time.gmtime(os.path.getmtime(file_path))) 
    print(timestamp_str, ' -->', file_path) 
This one fails Sad
import os
import glob
from pathlib import Path

dr = "C:/01"
for esbd in Path(dr).iterdir() :
    if esbd.is_dir() :

        dir_name = str(esbd)
        print(f"-- {dir_name}")
        list_of_files = filter( os.path.isfile,
                                glob.glob(dir_name + '*') )
        list_of_files = sorted( list_of_files,
                                key = os.path.getmtime)
        for file_path in list_of_files:
            timestamp_str = time.strftime(  '%m/%d/%Y :: %H:%M:%S',
                                        time.gmtime(os.path.getmtime(file_path))) 
            print(timestamp_str, ' -->', file_path) 
Thank you in advance!
Reply
#2
By the way, is the
time.gmtime
gives the modification time?

Thank you!
Reply
#3
this one also looks ok but fails to print sorted files for each subdirectory.

dr = 'c:/01/'
for esbd in Path(dr).iterdir() :
    if esbd.is_dir() :
        #rint(f" SubDir -> {esbd}")
        ddr = str(esbd)
        files = glob.glob(os.path.expanduser(ddr +"*.txt"))
        sorted_by_mtime_ascending = sorted(files, key=lambda t: os.stat(t).st_mtime)
        sorted_by_mtime_descending = sorted(files, key=lambda t: -os.stat(t).st_mtime)
        for ef in sorted_by_mtime_descending :
            print (f" === {ef}")
Reply
#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
#5
Got it! Great answer, as usual, you guys are great!
Thank you...
Reply


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