Python Forum
why i don't like os.walk()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why i don't like os.walk()
#9
so ...

the [:] on the LHS uses a whole list slice assignment to update the existing reference and avoid assigning a new reference. and each dirlist reference i get from each step of the generator i get from os.walk() is kept internal in the generator and subsequently used, as modified in-place, to descend in the tree walk.

(Jan-09-2018, 05:09 PM)wavic Wrote: Well, if I want to traverse my /home/$USER directory and I want to exclude Music and Video I can just remove them from the list:

for root, dirs, files in os.walk('/home/victor/'):
    dirs = [item for item in dirs[:] if item not in ('Music', 'Video)]
It's completely safe to do that and os.walk will not going through these directories.

so ...
for root, dirs, files in os.walk('/home/victor/'):
    dirs[:] = [item for item in dirs if item not in ('Music', 'Video)]
it builds a new list without those 2 names, which is thus 2 elements shorter, and is copied (because of the slice reference on the LHS) into the whole list (because the slice reference is a whole reference) making that list to now be 2 elements shorter.

or i could do ...
for root, dirs, files in os.walk('/home/victor/'):
    dirs[:] = sorted([item for item in dirs if item not in ('Music', 'Video)])
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
why i don't like os.walk() - by Skaperen - Jan-09-2018, 05:50 AM
RE: why i don't like os.walk() - by Larz60+ - Jan-09-2018, 06:35 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-09-2018, 07:44 AM
RE: why i don't like os.walk() - by Skaperen - Jan-09-2018, 08:39 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-09-2018, 08:48 AM
RE: why i don't like os.walk() - by DeaD_EyE - Jan-09-2018, 08:53 AM
RE: why i don't like os.walk() - by wavic - Jan-09-2018, 05:09 PM
RE: why i don't like os.walk() - by Gribouillis - Jan-09-2018, 05:18 PM
RE: why i don't like os.walk() - by Skaperen - Jan-10-2018, 03:26 AM
RE: why i don't like os.walk() - by Skaperen - Jan-10-2018, 04:44 AM
RE: why i don't like os.walk() - by Larz60+ - Jan-10-2018, 05:03 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-10-2018, 05:12 AM
RE: why i don't like os.walk() - by Skaperen - Jan-10-2018, 05:54 AM
RE: why i don't like os.walk() - by Skaperen - Jan-10-2018, 07:01 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-10-2018, 10:45 PM
RE: why i don't like os.walk() - by snippsat - Jan-10-2018, 11:59 PM
RE: why i don't like os.walk() - by Skaperen - Jan-11-2018, 01:37 AM
RE: why i don't like os.walk() - by Larz60+ - Jan-11-2018, 02:23 AM
RE: why i don't like os.walk() - by Skaperen - Jan-11-2018, 06:00 AM
RE: why i don't like os.walk() - by Gribouillis - Jan-11-2018, 07:07 AM
RE: why i don't like os.walk() - by Skaperen - Jan-11-2018, 08:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  examples using os.walk() Skaperen 12 7,611 Mar-22-2021, 05:56 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020