"rbt" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Could someone demonstrate the correct/proper way to use os.walk() to skip
> certain files and folders while walking a specified path? I've read the
> module docs and googled to no avail and posted here about other os.walk
> issues, but I think I need to back up to the basics or find another tool
> as this isn't going anywhere fast... I've tried this:
>
> for root, dirs, files in os.walk(path, topdown=True):
>
> file_skip_list = ['file1', 'file2']
> dir_skip_list = ['dir1', 'dir2']
>
> for f in files:
> if f in file_skip_list
> files.remove(f)
>
> for d in dirs:
> if d in dir_skip_list:
> dirs.remove(d)
I think the problem here is that you are removing elements from a list while
traversing it. Try to use a copy for the traversal, like this:
for f in files[:]:
if f in file_skip_list
files.remove(f)
for d in dirs[:]:
if d in dir_skip_list:
dirs.remove(d)
> And This:
>
> files = [f for f in files if f not in file_skip_list]
> dirs = [d for d in dirs if dir not in dir_skip_list]
This is not doing what you want because it just creates new lists and it
doesn't modify the existing lists that the os.walk generator is using.
--
http://mail.python.org/mailman/listinfo/python-list