Python Forum
Latest file with a pattern produces an error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Latest file with a pattern produces an error
#1
Hi,
I'm trying to find the latest file in a directory but only if a file has a digit in its name.
I globbed the files that have "digit/digits" in the name but after adding "max(fname, key=os.path.getctime)"
The script produces a bunch of errors Confused .

import os
import glob

for fname in glob.glob('C:\\02\\*[0-9].*'):
    print (fname)
    lt_file = max(fname, key=os.path.getctime)
    
    print ("Latest file -->>", lt_file)
Thank you.
Reply
#2
I take it from your glob statement that the digit must come on the end of the filename stem (part before the .) is that correct?
tester_V likes this post
Reply
#3
fname is set repeatedly to each file in turn inside the loop. You can't ask for the max of one thing.

You can ask for the max of all of them together though.

g = glob.glob('C:\02\*[0-9].*')
lt_file = max(g, key=os.path.getctime)
print(f"Latest file -->> {lt_file}")
tester_V likes this post
Reply
#4
(Dec-09-2020, 02:11 AM)Larz60+ Wrote: I take it from your glob statement that the digit must come on the end of the filename stem (part before the .) is that correct?

That is correct.
Line this, "something01.log"
Reply
#5
(Dec-09-2020, 04:23 AM)bowlofred Wrote: fname is set repeatedly to each file in turn inside the loop. You can't ask for the max of one thing.

You can ask for the max of all of them together though.

g = glob.glob('C:\02\*[0-9].*')
lt_file = max(g, key=os.path.getctime)
print(f"Latest file -->> {lt_file}")

I see your point!
I thought I should iterate over the files to find the oldest one.

Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get latest version off website and save it as variable [SOLVED] AlphaInc 5 1,943 Nov-14-2021, 09:00 PM
Last Post: DeaD_EyE
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 4,381 Aug-13-2021, 08:21 PM
Last Post: Winfried
  Running latest Python version on the Terminal (MAC) Damian 4 2,635 Mar-22-2021, 07:58 AM
Last Post: Damian
  Read plotly-latest.min.js from local issac_n 1 2,174 Nov-18-2020, 02:08 PM
Last Post: issac_n
  How to save latest time stamp in a file? redwood 12 7,210 Jul-11-2019, 11:03 AM
Last Post: redwood
  grep command based on lines in a pattern file Skaperen 1 2,190 Jan-07-2019, 10:23 AM
Last Post: Gribouillis
  modify line in file if pattern found in list. kttan 1 2,214 Dec-10-2018, 08:45 AM
Last Post: Gribouillis
  Python ftp server get the latest sub-directory name muzamalrana 1 3,440 Aug-08-2018, 11:40 PM
Last Post: muzamalrana
  How to install latest version of 'pygame' sylas 1 10,319 May-19-2018, 10:03 AM
Last Post: snippsat
  Type hint produces Syntax error Digamma 4 10,429 Apr-27-2018, 06:50 AM
Last Post: Digamma

Forum Jump:

User Panel Messages

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