Path / Listing and os.walk problem.

Peter Otten __peter__ at
Thu Aug 26 09:54:13 CEST 2010

Alban Nona wrote:

> Hi
> So here is my problem:
> I have my render files that are into a directory like this:
> c:\log\renderfiles\HPO7_SEQ004_031_VDM_DIF_V001.0001.exr
> c:\log\renderfiles\HPO7_SEQ004_031_VDM_DIF_V001.0002.exr
> c:\log\renderfiles\HPO7_SEQ004_031_VDM_DIF_V001.0003.exr
> ....
> c:\log\renderfiles\HPO7_SEQ004_031_VDM_AMB_V001.0001.exr
> c:\log\renderfiles\HPO7_SEQ004_031_VDM_AMB_V001.0002.exr
> c:\log\renderfiles\HPO7_SEQ004_031_VDM_AMB_V001.0003.exr
> True is, there is like 1000 Files is the directory (C:\log\renderfiles\)
> What Iam looking to is to extract the first part of the filenames as a
> list, but I dont want the script to extract it 1000times, I mean I dont
> need it to extract HPO7_SEQ004_031_VDM_AMB 150 times, because there is 150
> Frames. (not sure if its clear tought)
> so far, I would like the list to look lik:
> ["HPO7_SEQ004_031_VDM_DIF", "HPO7_SEQ004_031_VDM_AMB", etc...]
> I start to think about that, to try to use a
> for (path, dirs, files) in os.walk(path):
>     list.append(files)
> but this kind of thing will just append the whole 1000 files, thing that I
> dont want, and more complicated I dont want the thing after "AMB" or "DIF"
> in the name files to follow.
> (thing I can delete using a split, if I read well ?)
> I trying to search on internet for answer, but seems I find nothing about
> it.
> Someone can help me with that please, show me the way or something ?

You can use glob. Assuming the files are all in one directory:

import os
import glob

folder = r"C:\log\renderfiles"

# find files that end with "_V001.0001.exr"
pattern = os.path.join(folder, "*_V001.0001.exr")
files = glob.glob(pattern)

# remove the directory
names = [os.path.basename(f) for f in files]

# remove everything after and including the last occurence of "_"
names = [n.rpartition("_")[0] for n in names]

print "\n".join(sorted(names))


More information about the Python-list mailing list