Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定文件的最新版本(python)_Python_File_Versioning - Fatal编程技术网

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

确定文件的最新版本(python),python,file,versioning,Python,File,Versioning,这让我很困惑 我有一个文件夹中的文件列表。例如 myFiles = ["apple_d_v01.jpg", "apple_d_v02.jpg", "apple_d_v03.jpg", "something_d.jpg", "anotherthing_d.jpg"] 文件“apple\u d”有三个版本,版本后缀为“\u vxx”。我希望能够修改列表,使其仅具有最新版本,以便 myFiles = ["apple_d_v03.jpg", "something_d.jpg", "anotherthi

这让我很困惑

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

myFiles = ["apple_d_v01.jpg", "apple_d_v02.jpg", "apple_d_v03.jpg", "something_d.jpg", "anotherthing_d.jpg"]
文件“apple\u d”有三个版本,版本后缀为“\u 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]

假设
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)

我所做的这个方法我想会满足你的要求,它获取一个文件名列表并查找最新版本,然后搜索所有包含版本标记的文件,并删除那些不是最新的文件。如果某些文件仅更新为版本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
输入示例:

名称列表=[“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”]

假设版本始终使用_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)
简短的(功能性的)回答如下:

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 ]

到目前为止你有什么?我已经发布了一个非常基本的答案。我可以改进,如果你张贴你所做的,这样我就可以更好地了解你在寻找什么
这让我很困惑
到目前为止你都尝试了什么?嗨,Dh0rse,谢谢你的输入。这看起来很不错,尽管有时肯定会有比其他版本进步更多的文件。如果您不介意再次快速进行修订,我将非常感谢您查看我的第二个解决方案,我相信这就是您所寻找的,它速度稍慢,因为它正在执行两个for循环,但它会获得您需要的信息。干杯。我也会看看这个。有一点时间让代码运行..谢谢Mario-我会研究这一点,并尝试消化你的编解码器pcm-我会检查这个解决方案。
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 ]