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


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 301 Apr-20-2024, 06:31 AM
Last Post: tester_V
  python print all files which contain specific word in it mg24 5 1,352 Jan-27-2023, 11:20 AM
Last Post: snippsat
  Failing regex tester_V 3 1,276 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  failing to print not matched lines from second file tester_V 14 6,341 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  set and sorted, not working how expected! wtr 2 1,356 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  Failing to connect to a host with WMI tester_V 6 4,540 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Failing to get Stat for a Directory tester_V 11 3,720 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Failing to Zip files tester_V 4 2,255 Dec-01-2020, 07:28 AM
Last Post: tester_V
  How to make elements return sorted? notsoexperienced 4 3,312 Sep-24-2020, 09:00 AM
Last Post: perfringo
  Why is my original list also sorted? Pedroski55 1 1,658 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