mcrash
mcrash

Reputation: 3

In python, how to get the path to all the files in a directory, including files in subdirectories, but excluding path to subdirectories

I have a directory containing folders and subfolders. At the end of each path there are files. I want to make a txt file containing the path to all the files, but excluding the path to folders.

I tried this suggestion from Getting a list of all subdirectories in the current directory, and my code looks like this:

import os

myDir = '/path/somewhere'

print [x[0] for x in os.walk(myDir)] 

And it gives the path of all elements (files AND folders), but I want only the paths to the files. Any ideas for it?

Upvotes: 0

Views: 2921

Answers (4)

Hackaholic
Hackaholic

Reputation: 19733

os.walk(path) returns three tuples parent folder, sub directories and files.

so you can do like this:

for dir, subdir, files in os.walk(path):
    for file in files:
        print os.path.join(dir, file)

Upvotes: 1

Fomalhaut
Fomalhaut

Reputation: 9737

def get_paths(path, depth=None):
    for name in os.listdir(path):
        full_path = os.path.join(path, name)

        if os.path.isfile(full_path):
            yield full_path

        else:
            d = depth - 1 if depth is not None else None

            if d is None or d >= 0:
                for sub_path in get_paths(full_path):
                    yield sub_path

Upvotes: 0

Martin Evans
Martin Evans

Reputation: 46759

If you just want the paths, then add a filter to your list comprehension as follows:

import os

myDir = '/path/somewhere'
print [dirpath for dirpath, dirnames, filenames in os.walk(myDir) if filenames] 

This would then only add the path for folders which contain files.

Upvotes: 0

idjaw
idjaw

Reputation: 26580

The os.walk method gives you dirs, subdirs and files in each iteration, so when you are looping through os.walk, you will have to then iterate over the files and combine each file with "dir".

In order to perform this combination, what you want to do is do an os.path.join between the directory and the files.

Here is a simple example to help illustrate how traversing with os.walk works

from os import walk
from os.path import join

# specify in your loop in order dir, subdirectory, file for each level
for dir, subdir, files in walk('path'):
    # iterate over each file
    for file in files:
        # join will put together the directory and the file
        print(join(dir, file))

Upvotes: 0

Related Questions