Path / Listing and os.walk problem.

Alban Nona python.koda at gmail.com
Thu Aug 26 14:41:49 EDT 2010


So I found a way to do it, maybe some people could be interested:

listNames = []
for n in names:
    listNames.append('_'.join(n.split('_')[:-1]))
#It will cut the last part of the file name convention

listNames = list(set(listNames)) #we Delete duplicates from the list, like
this we only have what we are interested in
for e in listNames: #Juste to check.
    print e

:)



2010/8/26 Alban Nona <python.koda at gmail.com>

> Hey ! Thank you guys !
> It help me a lot !
>
> @Dennis (Gomes): Thanks ! I tried it it worked well but list me the whole
> files :P (and finally crashed python...lol)
>
> I looked at the Peter method and, Im really dumb to didnt tough about
> defining a pattern like *_v001.0001.exr * like this, it sort me only one
> frame...which is perfect and less memory consuming I guess.
>
> And glob use seems to be perfect for what I want to do ! so thank you to
> point me in this direction :p
>
> I tried also the Peter code, and it give me a good listing of my element
> like I wanted:
>
> HPO7_SEQ004_031_VDMRoom_ALB
> HPO7_SEQ004_031_VDMRoom_AMB
> HPO7_SEQ004_031_VDMRoom_BTY
> HPO7_SEQ004_031_VDMRoom_Cutouts_ALB
> HPO7_SEQ004_031_VDMRoom_Cutouts_AMB
> HPO7_SEQ004_031_VDMRoom_Cutouts_DET
> ....
> HPO7_SEQ004_031_VDMRoom_DET
> HPO7_SEQ004_031_VDMRoom_DIF
> HPO7_SEQ004_031_VDMRoom_DPF
> HPO7_SEQ004_031_VDMRoom_Decals_ALB
> ....
> HPO7_SEQ004_031_VDM_ALB
>
> HPO7_SEQ004_031_VDM_AMB
> HPO7_SEQ004_031_VDM_BTY
>
> HPO7_SEQ004_031_VDM_DIF
> HPO7_SEQ004_031_VDM_DPF
> HPO7_SEQ004_031_VDM_Fresnel_mat
>
>
> Unfortunatly, a new problem come to me, I looking to get that kind of
> list:
>
> HPO7_SEQ004_031_VDMRoom
> HPO7_SEQ004_031_VDMRoom
> HPO7_SEQ004_031_VDMRoom
> HPO7_SEQ004_031_VDMRoom_Cutouts
> HPO7_SEQ004_031_VDMRoom_Cutouts
> HPO7_SEQ004_031_VDMRoom_Cutouts
> ....
>
> Right now, Im looking the documentation to find a way to do it, Im thinking
> about string methods, I also though: " hey, I just have to delete the 4 last
> characters, but na ! itll not work because sometime I have something like
> "_Fresnel_mat'' which is of course more than 4 chars...)
>
> Maybe the best would be to declare something like "in the string, look at
> the last "_" and delete it + whatever there is after"
> but I didnt find how to do it, I mean I tried splitext which is, not
> appropriate.
>
> Do I have to declare a list of element like:
> elementList: ["_ALB", "AMB", "_Beauty", etc...]
> and to search that pattern in the files name to remove it after ? it seems
> not bad as solution, but I pretty sure there is a better way to do it.
>
> right ?
>
> anyway, thank very much guys ! :)
> and have a good day !
>
>
> 2010/8/26 Peter Otten <__peter__ at web.de>
>
> 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))
>>
>> Peter
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100826/43da7e0e/attachment-0001.html>


More information about the Python-list mailing list