Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8728929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:44:19+00:00 2026-06-13T08:44:19+00:00

Trying to enumerate all files in a certain directory (like ‘find .’ in Linux,

  • 0

Trying to enumerate all files in a certain directory (like ‘find .’ in Linux, or ‘dir /s /b’ in Windows).

I came up with the following nested list comprehension:

from os import walk
from os.path import join
root = r'c:\windows'      #choose any folder here
allfiles = [join(root,f) for f in files for root,dirs,files in walk(root)]

Unfortunately, for the last expression, I’m getting:

NameError: name 'files' is not defined

Related to this question, which (although working) I can’t understand the syntax of the nested list comprehension.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:44:20+00:00Added an answer on June 13, 2026 at 8:44 am

    You need to reverse the nesting;

    allfiles = [join(root,f) for root,dirs,files in walk(root) for f in files]
    

    See the list comprehension documentation:

    When a list comprehension is supplied, it consists of a single expression followed by at least one for clause and zero or more for or if clauses. In this case, the elements of the new list are those that would be produced by considering each of the for or if clauses a block, nesting from left to right, and evaluating the expression to produce a list element each time the innermost block is reached.

    In other words, since you basically want the moral equivalent of:

    allfiles = []
    for root, dirs, files in walk(root):
        for f in files:
            allfiles.append(f)
    

    your list comprehension should follow the same ordering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to enumerate all open files on a Windows system in C#. I cannot
Consider the following graph: I'm trying to find a way to enumerate all possible
Possible Duplicate: Enumerating threads in Windows I am trying to enumerate all threads belonging
I'm trying to enumerate all the permissions used by all the installed packages, however
I am trying to enumerate all the namespaces in a document using XPath. The
I'm trying to enumerate through all the Controls of a Page, but all I
I'm trying to enumerate all methods in an assembly and add them to nodes
I'm trying to enumerate all hosts in an XML document, create a db entry
I am trying to write a Windows application that gives me back all the
I am trying to list the files in all the sub-directories of a root

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.