need insight...

Stephen Hansen stephen at cerebralmaelstrom.com
Sat Jun 24 04:27:37 EDT 2000


    The problem is that you really do not want to change the sequence you're
using in 'for' -- the documentation, if memory serves, shows an example of
precisely why. You'll want to make a copy of it to iterate through.. like;

for item in cwd[:]:
    ...
    cwd.remove(item)

Like that..see? The [:] slice copies the whole sequence.

--Stephen

David Broadwell <dbroadwell at mindspring.com> wrote in message
news:3954671F.51E53AE6 at mindspring.com...
> The code to follow, to what i know, is behaving erratically. it is
> erratic in that in the section after the print 'checking... statement
> the  'for item in directorylist:' seems to be skipping over entries in
> it's list for no apparent reason... (note: what is skipps changes if the
> list it is given chages, but from what i can tell, it always skips about
> half of the things on the list. i have snipped out as much of the code
> as i can and still leave it psudo functional..
>
> I am seeking help in some capacity, and if i have commited some grevious
> programming error, enough information to avoid it in the future..
>
> If it dosen't demonstate the same problem i will post the code entire
> (it is about 60 lines)
>
> thanks!
>
> # Code follows..
>
> import os
> import sys
>
> filetype = 'm3u'
> directorylist = []
>
> def doit():
>     cwd = []
>     print 'reading current directory'
>     cwd = os.listdir('.')
>     print 'building directorylist'
>     for item in cwd:
>         if (os.path.isdir(item)):
>            directorylist.append(item)
>            cwd.remove(item)
>     del cwd
>     print 'Checking for subdirectories to check.'
>     print directorylist
>     for item in directorylist:
>         print 'subdirectory ' + item + ' found, scanning.'
>         os.chdir(item)
>         subdir = []
>         subdir = os.listdir('.')
>         print subdir
>         for thing in subdir:
>             print thing,
>             if (thing[(len(thing)-3):] == filetype):
>                print 'Yup.'
>                # join item(path) to file
>                # add to filelist
>             else:
>                print 'Nope.'
>                subdir.remove(thing)
>         print subdir
>         del subdir
>         os.chdir('..')
>
> doit()
>
> --
>         ~We're all fallen angels who've forgotten how to fly~
>         Remember how: http://www.cyber-action.com/oed/dbroadwell.html
> --





More information about the Python-list mailing list