user23469
user23469

Reputation: 11

os.walk on non C drive directory

I know there are several posts that touch on this, but I haven't found one that works for me yet. I need to create a list of files with an .mxd extension by searching an entire mapped directory. I used this code and it works:

    import os
    file_list = []
    for (paths, dirs, files) in os.walk(folder):
        for file in files:
            if file.endswith(".mxd"):
                file_list.append(os.path.join(paths, file))

However, it only works on the C drive. I need to be able to search for these files on a mapped drive of my choosing. Here's the script I'm using, but it doesn't work. I know there is an mxd file in the subdirectory of this drive, but it isn't being reported in the file list. In fact, the file list is totally empty, and it shouldn't be.

    import os
    path = r"U:/TEST/"
    filenamelist = []
    for files in os.walk(path):
         if file.endswith(".mxd"):
            filenamelist.append(files)

Does someone see anything wrong in my second block of code that would provent it from iterated through subdirectories at the given path and reporting back files with an .mxd extension?

Upvotes: 0

Views: 2458

Answers (3)

user23469
user23469

Reputation: 11

Here's the final script that worked for crawling on a drive root other than C:

import os
file_list = []
path = r'U:\\'
for (dirpath, subdirs, files) in os.walk(path):
    for file in files:
        if file.endswith(".mxd"):
            file_list.append(os.path.join(dirpath, file))

Upvotes: 0

abarnert
abarnert

Reputation: 366133

Does someone see anything wrong in my second block of code that would provent it from iterated through subdirectories at the given path and reporting back files with an .mxd extension?

Yes. Compare and contrast your two loops:

for (paths, dirs, files) in os.walk(folder):
    for file in files:
        if file.endswith(".mxd"):
            file_list.append(os.path.join(paths, file))

for files in os.walk(path):
     if file.endswith(".mxd"):
        filenamelist.append(files)

You're trying to do the exact same thing, but not using even remotely the same code.

In the first one, you loop over the walk, storing each tuple in (paths, dirs, files), then loop over files.

In the second one, you loop over the walk, storing each tuple in files, don't loop over anything, and then just use some variable named file left over from some earlier code.

And then, even if that part worked, you end up appending files (which, remember, is a tuple of three lists) rather than file—or, probably better, os.path.join(paths, file)—to the list.

Just make the second one look like the first. Or, better, put it in a function and call it twice, instead of copying and pasting it.

Upvotes: 1

unutbu
unutbu

Reputation: 880887

os.walk(path) yields a 3-tuple which is commonly unpacked as root, dirs, files. So instead of

for files in os.walk(path):
    ...

use

for root, dirs, files in os.walk(path):
    for filename in files:
        if filename.endswith(".mxd"):
            filenamelist.append(filename)

Upvotes: 1

Related Questions