[Tutor] lists and s.split

Michael Janssen Janssen@rz.uni-frankfurt.de
Fri Mar 21 07:09:02 2003


On Thu, 20 Mar 2003, Not Important wrote:
> import os
> import string
>
> os.chdir("dir")
>
> list = os.listdirs("dir")
>
> for name in list
>     dir_name string.split(name " - ")[1]
>
> i can only get dir_name to represent one of the file names in directory
but i would like it to be a list of all the names
>
> how if possible.

dir_name_list = [] # create it early, otherwise "+=" won't work
for name in list:
    dir_name_list += string.split(name, "-")[1]

NB: [1] takes the *second* split-string.

"+=" needs recent version of python, otherwise:
    dir_name_list = dir_name_list + string.split(name, "-")[1]

Michael
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!