Determining the latest version of a file (python) -


this has me stumped...

i have list of files in folder. eg.

myfiles = ["apple_d_v01.jpg", "apple_d_v02.jpg", "apple_d_v03.jpg", "something_d.jpg", "anotherthing_d.jpg"] 

there 3 versions of file "apple_d", using version suffix of "_vxx". want able modify list have latest version,

myfiles = ["apple_d_v03.jpg", "something_d.jpg", "anotherthing_d.jpg"] 

any ideas?

thanks much.

edit: came thismorning- works fine purpose, little different question asked. helping out.

myfiles = ["apple_d.jpg", "apple_dm.jpg", "apple_d_v2.jpg", "apple_d_v3.jpg", "something_d.jpg", "anotherthing_d.jpg", "test2_s_v01", "test2_s_v02.jpg", "test2_s_v03.jpg", "test2_s_v04.jpg" ]  objversions = []   obj = "cube"  #controlled variable suf = "d"     #controlled variable ext = ".jpg"  #controlled variable  file in myfiles:       if obj + "_" + suf + "_" in file:         objversions.append(file)      if obj + "_" + suf + "." in file:         objversions.append(file)  objversions = sorted(objversions, reverse=true)  file in objversions:      if ext not in file:         objversions.remove(file)   chosenfile = objversions[0] 

assuming d version number in question

latestversion = max(int(fname.rsplit('.',1)[0].rsplit("_",1)[1].strip('v')) fname in myfiles) 

from comments, understand want keep latest versions of versioned files. that, you'll need this:

answer = set() fname in myfiles:     name, version = fname.rsplit('.', 1)[0].rsplit("_",1)     if version.startswith('v'): # versioned file         answer.add(           max((fname fname in myfiles if fname.startswith(name) , not fname.rsplit('.', 1)[0].endswith('d')),                key=lambda fname: int(                      fname.rsplit('.', 1)[0].rsplit("_",1)[1].strip('v')) ))     else:         answer.add(fname) 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -