确定文件的最新版本(python)

时间:2013-07-31 21:08:22

标签: python file versioning

这让我难过......

我有一个文件夹中的文件列表。例如

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

文件“apple_d”有三个版本,使用版本后缀“_vxx”。我希望能够将列表修改为仅包含最新版本,以便

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

有什么想法吗?

非常感谢。

编辑:提出这个问题 - 它的目的很好,但与我最初提出的问题有点不同。谢谢大家帮忙。

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 by variable
suf = "d"     #controlled by variable
ext = ".jpg"  #controlled by variable

for file in myFiles:


    if obj + "_" + suf + "_" in file:
        objVersions.append(file)

    if obj + "_" + suf + "." in file:
        objVersions.append(file)

objVersions = sorted(objVersions, reverse=True)

for file in objVersions:

    if ext not in file:
        objVersions.remove(file)


chosenfile = objVersions[0]

4 个答案:

答案 0 :(得分:0)

假设d是您问题中的版本号

latestVersion = max(int(fname.rsplit('.',1)[0].rsplit("_",1)[1].strip('v')) for fname in myFiles)

根据您的评论,我了解您希望保留最新版本的版本化文件。为此,你需要这个:

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

答案 1 :(得分:0)

这个方法我认为我会做你所要求的,它需要一个文件名列表并找到最新版本,然后搜索包含版本标签的所有文件,并删除那些不是最新版本的文件。如果某些文件仅更新为版本2而其他文件仅更新为3,则无效。

def removePreviousVersions(FileNameList):
    returnList = []
    LatestVersion = 0
    for FileName in FileNameList:
        if FileName.find('_v') > -1:
            Name, Version = (FileName.replace('.jpg', '')).split('_v')
            if LatestVersion <  int(Version):
                LatestVersion = int(Version)

    argument = '_v'+ str(LatestVersion).zfill(2)
    for FileName in FileNameList:
        if FileName.find('_v') == -1:
            returnList.append(FileName)        
        elif FileName.find(argument) != -1:
            returnList.append(FileName)

    return returnList

此处的示例使用类似的方法,但它会获取文件的最新版本,即使它是较旧版本的文件的最新版本。

def removePreviousVersions(FileNameList):
TempFileNameList = []
ReturnList = []  
for FileName in FileNameList:
    if '_v' in FileName:
        Name, Version = (FileName.replace('.jpg', '')).split('_v')
        if Name not in TempFileNameList:
            TempFileNameList.append(Name)
            latestVersion = 0
            TempFileName = ''
            for fname in FileNameList:
                if Name in fname:
                    tName, tVersion = (fname.replace('.jpg', '')).split('_v')
                    if int(tVersion) > int(latestVersion):
                        latestVersion = int(tVersion)
                        TempFileName = fname
            ReturnList.append(TempFileName)
    else:
        ReturnList.append(FileName)

return ReturnList

示例输入:

NameList = [“stupd_d_v01.jpg”,“apple_d_v01.jpg”,“apple_d_v02.jpg”,“apple_d_v03.jpg”,“something_d.jpg”,“anotherthing_d.jpg”]

返回[“stupd_d_v01.jpg”,“apple_d_v03.jpg”,“something_d.jpg”,“anotherthing_d.jpg”]

答案 2 :(得分:0)

假设版本总是使用_v ##的语法,您可以这样做:

import re

parts_re = re.compile(r'^(.+_d)(.*)\.jpg$')

def remove_oldies(list):
    final = []
    saved_version = ''
    saved_name = ''
    for item in sorted(list):
        name, version = parts_re.search(item).group(1,2)
        if name != saved_name:
            if saved_name != '':
                final.append(saved_name + saved_version + ".jpg")
            saved_version = version
            saved_name = name
        else:
            saved_version = version
    final.append(saved_name + saved_version + ".jpg")
    return final

    remove_oldies(myFiles)

答案 3 :(得分:0)

简短(和功能性)答案如下:

files= [ (f.split("_d")[0],int("0"+re.search("((_v)?([0-9]+|))\.jpg",f.split("_d")[1]).group(3)),f) for f in myFiles]
result= [ [ f[2] for f in files if f[0] == fn and f[1] == max( [ f[1] for f in files if f[0] == fn ] ) ][0] for fn in set( f[0] for f in files ) ]


随着一些扩展和评论添加:

# List of tuples of the form ('apple', 2, 'apple_d_v02.jpg') and ('something', 0, 'something_d.jpg')
files= [ (f.split("_d")[0],int("0"+re.search("((_v)?([0-9]+|))\.jpg",f.split("_d")[1]).group(3)),f) for f in myFiles]
basename= 0  # index of basename (apple, something, etc) in each tuple inside "files"
version= 1  # index of version in each tuple inside "files"
fullname= 2  # index of full filename in each tuple inside "files"

result= [ [ f[fullname] for f in files if f[basename] == current_basename and f[version] == max( [ f[version] for f in files if f[basename] == current_basename ] ) ][0] for current_basename in set( f[basename] for f in files ) ]


最后一行可以进一步扩展为:

def max_version_fullname(current_basename):
    versions= [ f[version] for f in files if f[basename] == current_basename ]
    max_version= max( versions )
    fullnames_for_max_version= [ f[fullname] for f in files if f[basename] == current_basename and f[version] == max_version ]
    fullname_for_max_version= fullnames_for_max_version[0]
    return fullname_for_max_version
basenames= set( f[basename] for f in files )
result= [ max_version_fullname(current_basename) for current_basename in basenames ]