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

  • Home
  • SEARCH
  • 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 4115084
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:32:12+00:00 2026-05-20T22:32:12+00:00

In Python, I want to list all files in a set of directories. The

  • 0

In Python, I want to list all files in a set of directories. The best I’d like to get is a list. But at most I managed to make a nested list:

pics = os.path.expanduser('~/Pictures')
all_pics = [(d, os.listdir(d)) for d in os.listdir(pics)]

result:

[('folder1', ['file1', 'file2', ...]), ('folder2', ['file1', ...]), ...]

what I want:

[('folder1' 'file1'), ('folder1', 'file2'), ..., ('folder2', 'file1'), ...]

What I would like to get is a simple plain list, doesn’t matter of what (can be of tuples), just so that it hasn’t nested things, and I need no nested cycles in the code that parses it.

How can I do this with list comprehensions? Doing this gives me a product of 2 sets (dir names and filenames) which is wrong:

[(d, f) for f in os.listdir(os.path.join(pics, d)) for d in os.listdir(pics)]
  • 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-05-20T22:32:13+00:00Added an answer on May 20, 2026 at 10:32 pm

    You got the order of the for-loops wrong. It should be

    all_pics = [(d, f)
                for d in os.listdir(pics)
                for f in os.listdir(os.path.join(pics, d))]
    

    Outermost loop first, innermost loop last.

    I wonder why you didn’t get a NameError for d.

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

Sidebar

Related Questions

How do I get a list of all python modules available? I don't want
I am trying to get filtered list of all Text and Python file, like
In Python, I only want to list all the files in the current directory
Possible Duplicate: How to generate all permutations of a list in Python I want
I want to wrap around a list/vector in C++ like in Python. Basically I
I have a python list which contains dictionaries and I want to make a
to get file which ended in . ,you can do : 1.R list.files(path='/home/test', all.files=TRUE,
I'd like to recursively walk a directory, but I want python to break from
I am trying to get a list of files in a directory using Python,
I want a python function that takes a pdf and returns a list of

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.