Nov-12-2022, 04:05 AM
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.
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
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!
