Python Forum

Full Version: Failing to print sorted files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
By the way, is the
time.gmtime
gives the modification time?

Thank you!
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}")
(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
Got it! Great answer, as usual, you guys are great!
Thank you...