Q:

Trouble with converting function to a one line list comprehension

Could someone explain to me the correct solution for converting this function to a one line list comprehension?
def get_files(directory):
    filepaths = []
    for root, dirs, files in os.walk(directory):
        for filename in files:
           filepaths.append(os.path.join(root, filename))
    return filepath

I've tried this but it doesn't seem to work (NameError, name 'files' not defined)
def get_files(directory):
    return [os.path.join(root, filename) for filename in files for root, dirs, files in os.walk(directory)]

A:

The for statements are in the wrong order. Try:
[os.path.join(root, filename) for root, dirs, files in os.walk(directory)
                              for filename in files]

