我有一个包含多个字符串的列表
myList = ['someFileName_red', 'someFileName_blue_v006', 'someFileName_green_v004']
我想使用此列表作为搜索条件,以查找服务器上具有匹配名称的图像序列。这些图像序列位于服务器上的所有位置。
我将从中开始搜索的根目录是:
rootPath = r'C:\Users\userName\Desktop\Seq'
我试图创建一个for循环,通过 myList 和 rootPath 变量搜索音量,但我发现很难返回结果for循环作为循环中的第一项。以下是我尝试做的事情的例子:
for root, dirs, files in os.walk(rootPath):
for x in shotList:
for file in files:
if file.startswith(x):
if file.endswith('.exr'):
print os.path.join(root,file)
返回:
C:\Users\userName\Desktop\Seq\shot\images\someFileName_red.0101.exr
C:\Users\userName\Desktop\Seq\shot\images\someFileName_red.0102.exr
C:\Users\userName\Desktop\Seq\shot\images\someFileName_red.0103.exr
C:\Users\userName\Desktop\Seq\shot\images\someFileName_red.0104.exr
C:\Users\userName\Desktop\Seq\shot\images\someFileName_red.0105.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_blue\V006\Images\someFileName_blue_v006.0101.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_blue\V006\Images\someFileName_blue_v006.0102.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_blue\V006\Images\someFileName_blue_v006.0103.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_blue\V006\Images\someFileName_blue_v006.0104.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_blue\V006\Images\someFileName_blue_v006.0105.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_blue\V006\Images\someFileName_blue_v006.0106.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_blue\V006\Images\someFileName_blue_v006.0107.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_green\V004\Images\someFileName_green_v004.0101.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_green\V004\Images\someFileName_green_v004.0102.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_green\V004\Images\someFileName_green_v004.0103.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_green\V004\Images\someFileName_green_v004.0104.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_green\V004\Images\someFileName_green_v004.0105.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_green\V004\Images\someFileName_green_v004.0106.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_green\V004\Images\someFileName_green_v004.0107.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_green\V004\Images\someFileName_green_v004.0108.exr
我想要回来的是图像序列的第一帧:
C:\Users\userName\Desktop\Seq\shot\images\someFileName_red.0101.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_blue\V006\Images\someFileName_blue_v006.0101.exr
C:\Users\userName\Desktop\Seq\shot\someFileName_green\V004\Images\someFileName_green_v004.0101.exr
答案 0 :(得分:0)
您需要在打印第一个文件后添加break
并显示:
for root, dirs, files in os.walk(rootPath):
for x in shotList:
for file in files:
if file.startswith(x) and file.endswith('.exr'):
print os.path.join(root,file)
break