[Tutor] trying to split or rpartition the contents of a list

Stuart Tozer stutozer at gmail.com
Wed May 22 09:49:07 CEST 2013


Hi everyone.

I'm stuck on a problem while developing a small tool for Maya. Basically, I
have a folder with some filenames in it which have been returned by
os.listdir().
These filenames look something like...


apple_d.jpg
apple_si.jpg
apple_sg.jpg
box_d.jpg
box_si.jpg
pumpkin_d.jpg


Right now, I'm using os.listdir to get the filenames, and then each
filename becomes a selectable option in an option menu in maya. The code
is...

objects = os.listdir(dir)





    for object in objects:
        cmds.menuItem(label = object, parent = "objectMenu")



My problem is that I don't want duplicates of the same object in the menu
and I also want to truncate everything past and including the underscore.
So for eg, if my filenames are the same as the example above, I want to
change the list so I have this instead...

apple
box
pumpkin


I have tried splitting the filenames, but so far haven't had any luck.

for object in objects:
        sorted(set(object.split('_', 1)[0]))
        cmds.menuItem(label = object, parent = "objectMenu")



Also, I tried .rpartition but also no luck. I'm very new to python and this
has me stumped.
Any suggestions would be most welcome! ;)

Cheers,
S Tozer




-- 
http://stutozer.elementfx.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130522/05a5c87a/attachment.html>


More information about the Tutor mailing list