简体   繁体   中英

Exclude root directories using os.walk

I'm trying to list all the files my laptop but I want to exclude some root directories.

For example: I have the follwoing files:

 /Users/teste/demo/file.csv
 /Users/teste/demo3/file.csv
 /Users/project/file.csv

What I want is to exclude all the files from /Users/teste/ . For that I have this code:

import os
exclude = ['/Users/teste/',]
for root, dirs, files in os.walk("\\", topdown=False):
    if root not in exclude:
        for name in files:
            print(name)

However, my code is printing the files from directory demo and demo3 because the root include the demo part. If I print the root I will get:

/Users/teste/demo 
/Users/teste/demo3 
/Users/project/

And I want to include only the /Users/project/file.csv file

How can I filter using the parent root?

You can use startswith with tuple (not list)

if not root.startswith( ('/Users/teste/', '/other/folder') ):

import os

exclude = ['/Users/teste/',]

exclude = tuple(exclude)

for root, dirs, files in os.walk("\\", topdown=False):
    if not root.startswith(exclude):
        for name in files:
            print(name)

BTW:

If you want to use function which can't get list or tuple then you can use any() with list comprehension to check all elements on list

For example for startswith()

if not any(root.startswith(x) for x in exclude):

or for regex (which can be useful to create more complex element in exclude )

if not any(re.findall(x, root) for x in exclude):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM