这让我难住...确定文件的最新版本(python)
我有一个文件夹中的文件列表。例如。
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]
到目前为止你有什么? – kindall
我发布了一个非常基本的答案。如果您发布了您所做的事情,我可以改进这一点,以便更好地了解您所寻找的内容。 '这让我难住'你到目前为止尝试过什么? – inspectorG4dget