Python Forum
Filer and sort files by modification time in a directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filer and sort files by modification time in a directory
#3
I would not lie, I Googled it and I do not understand it. I actually come up with a "readable' one and it works.
I'm not sure if it is a good code or not. Sorry about that!
lst_ogs = []

for item in Path(host_path).iterdir() :
	if item.is_file() :
		fname = Path(item).name
		#print(f" .. {fname}")
		if fname.startswith("some_log") : 
			lst_ogs.append(item)            

lst_ogs.sort(key=os.path.getmtime,reverse=True) 
Thank you!
Reply


Messages In This Thread
RE: Filer and sort files by modification time in a directory - by tester_V - May-02-2024, 06:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  deleting files in program files directory RRADC 6 3,755 Aug-21-2024, 06:11 PM
Last Post: snippsat
  Loop through all files in a directory? Winfried 10 5,391 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 1,511 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 3,077 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  change directory of save of python files akbarza 3 3,942 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 8,615 Jun-27-2023, 01:17 PM
Last Post: diver999
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 2,958 May-07-2023, 12:33 PM
Last Post: deanhystad
  Read directory listing of files and parse out the highest number? cubangt 5 5,754 Sep-28-2022, 10:15 PM
Last Post: Larz60+
Photo a.sort() == b.sort() all the time 3lnyn0 1 2,118 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  How to save files in a separate directory Scordomaniac 3 16,499 Mar-16-2022, 10:17 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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