我正在编写一个脚本来检查文件列表中的图像序列。到目前为止,它运行得相当好,但由于某种原因,它总是跳过每个新序列的第一个文件。到目前为止,这是我的代码:
import os, re
filelist = ["A01B02_0123_A333.01234321.ari", "A01B02_0123_A333.01234322.ari", "A01B02_0123_A333.01234323.ari", "A01B02_0123_A333.01234324.ari", "A01B02_0123_A333.01234325.ari", "A01B02_0123_A333.01234326.ari", "A01B02_0123_A333.01234327.ari", "A01B02_0123_A333.01234328.ari", "A01B02_0123_A333.01234329.ari", "A01B02_0123_A333.01234330.ari", "A01B02_0123_A333.01234331.ari", "A02B03_0123_A444.0052.ari", "bus_v01.1001.tif", "bus_v01.1002.tif", "bus_v01.1003.tif", "bus_v02.1001.tif", "bus_v02.1002.tif", "bus_v02.1003.tif", "elementA_1001.jpg", "elementA_1002.jpg", "elementA_1003.jpg", "elementB_1001.jpg", "elementB_1002.jpg", "elementB_1003.jpg"]
def digitgroups(file):
padding_expr = re.compile("\d+")
lastfile = ""
sequence_index = 0
for file in filelist:
if file.endswith((".dpx", ".ari", ".jpg", ".tif")):
match = re.findall(padding_expr, file)[::-1]
lastmatch = re.findall(padding_expr, lastfile)[::-1]
if len(match) == len (lastmatch) and file != lastfile:
for i in range (0, len(match)):
digit_difference = int(lastmatch[i])-int(match[i])
if not (digit_difference) == 0:
if digit_difference == -1 or digit_difference == 1 and os.path.splitext(file)[-1] is os.path.splitext(l)[-1]:
print file, "a is part of sequence {0}".format(sequence_index)
elif digit_difference < -1 or digit_difference > 1:
sequence_index += 1
break
lastfile = file
digitgroups(filelist)
答案 0 :(得分:1)
除了评论中提到的所有内容外,循环依赖于lastfile
不为空。
第一遍:
if len(match) == len (lastmatch) and file is not lastfile:
在第一次传递时不会成立,因为lastfile设置为""
而len(lastmatch)
将设为0
。
此外,您正在传递list
,然后在函数中调用file
。
下面的解决方案对我有用(EDITED,更完整的解决方案):
import os, re
mylist = ["A01B02_0123_A333.01234321.ari", "A01B02_0123_A333.01234322.ari", "A01B02_0123_A333.01234323.ari", "A01B02_0123_A333.01234324.ari", "A01B02_0123_A333.01234325.ari", "A01B02_0123_A333.01234326.ari", "A01B02_0123_A333.01234327.ari", "A01B02_0123_A333.01234328.ari", "A01B02_0123_A333.01234329.ari", "A01B02_0123_A333.01234330.ari", "A01B02_0123_A333.01234331.ari", "A02B03_0123_A444.0052.ari", "bus_v01.1001.tif", "bus_v01.1002.tif", "bus_v01.1003.tif", "bus_v02.1001.tif", "bus_v02.1002.tif", "bus_v02.1003.tif", "elementA_1001.jpg", "elementA_1002.jpg", "elementA_1003.jpg", "elementB_1001.jpg", "elementB_1002.jpg", "elementB_1003.jpg"]
def digitgroups(files):
padding_expr = re.compile("\d+")
lastfile = ""
sequence_index = 0
for f in files:
if f.endswith((".dpx", ".ari", ".jpg", ".tif")):
match = re.findall(padding_expr, f)[::-1]
lastmatch = re.findall(padding_expr, lastfile)[::-1]
if len(match) == len(lastmatch) and f is not lastfile:
for i in range (0, len(match)):
digit_difference = int(lastmatch[i])-int(match[i])
if not digit_difference == 0:
if digit_difference == -1 or digit_difference == 1 and os.path.splitext(f)[-1] is os.path.splitext(l)[-1]:
print(f, "a is part of sequence {0}".format(sequence_index))
elif digit_difference < -1 or digit_difference > 1:
sequence_index += 1
break
else:
print(f, "a is part of sequence {0}".format(sequence_index))
lastfile = f
digitgroups(mylist)
注意lastfile = files.pop(0)
。这将从files
列表中获取第一个元素。