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 9188749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:05:19+00:00 2026-06-17T20:05:19+00:00

I have a directory containing some files, some directories, some symlinks to files and

  • 0

I have a directory containing some files, some directories, some symlinks to files and some symlinks to directories.

When I do os.walk() with followlinks=false in the directory, I get the files and symlinks to files in the filenames list and the directories in the dirnames list. But the symlinks to directories does not show up anywhere. Is this a bug or a feature in Python, or am I doing something wrong?

I expect the symlinks to directories to show up in the filenames list, because they are not directories but symlinks, and the other symlinks (to other files) show up in the filenames list.

Example:
The directory foo contains the following:

-rw-rw-r-- 4 rikno staff 136 Jan 14 11:10 firefox
lrwxr-xr-x 1 rikno staff   5 Jan 23 13:29 latex -> tetex
lrwxr-xr-x 2 rikno staff  68 Jan 14 11:10 mozilla -> firefox
drwxrwxr-x 3 rikno staff 102 Jan 23 13:29 tetex

I expect os.walk(‘foo’) in the first iteration to return

('foo', ['tetex'], ['firefox', 'latex', 'mozilla'])

or at least

('foo', ['latex', 'tetex'], ['firefox', 'mozilla'])

but all I get is

('foo', ['tetex'], ['firefox', 'mozilla'])

and I never get anything about the symlink latex (which points to the directory tetex)

Solved:

Ok, the result is

('foo', ['latex', 'tetex'], ['firefox', 'mozilla'])

So the symlink to the directory shows up in the dirnames list.

I first expected the symlink to the directory to be in the filenames list and never looked in the dirnames list, and when experimenting with the code and the file system to find where the link was or why the link was “missing” I accidently mixed up my results.

Sorry for asking.

  • 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-17T20:05:20+00:00Added an answer on June 17, 2026 at 8:05 pm

    Running on my machine, os.walk() does show all the sym links:

    >>> os.walk("foo").next()
    ('foo', ['tetex', 'latex'], ['mozilla', 'firefox'])
    >>> os.walk("foo", followlinks=False).next()
    ('foo', ['tetex', 'latex'], ['mozilla', 'firefox'])
    

    The only ‘problem’ I see here is that the sym link appears in the list of directories and not in the list of files.

    In most use cases, this would be the expected behaviour since one would expect to be able to treat all entries in the file-list as files and not have to check if it is a sym-link or not.

    This thread from python-dev briefly discusses the issue.

    “… putting the symlinks-to-directories
    into the files list instead of the subdirectory list isn’t really any
    better (it just moves the problem to different use cases, such as
    those that actually want to read the file contents).”

    and from the linked issue page:

    “For example to count the number of lines of all the files under a
    directory, a code could go like this:

    for root, dirs, files in os.walk(top):
        for file in files:
            f = open(file)
            for n, l in enumerate(f, 1):
                pass
            print(file, n)
    

    If, suddently, a symlink to a directory appeared in files, this will
    break. So I’m not convinced it’s worth changing this. A symlink to a
    directory is not much closer to a file than to a directory, it really
    depends on the use case.”

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

Sidebar

Related Questions

I have a directory containing several files, some of which have spaces in their
I have some directories containing test data, typically over 200,000 small (~4k) files per
I have a directory structure containing some files. I'm trying to find the names
I have a directory containing multiple subdirectories. I want to list only those subdirectories
I have a directory containing over 100 html files. I need to extract only
I have a highly structured hierarchical directory containing multiple files that need to be
I have a directory of text-based, compressed log files, each containing many records. In
I have some files in a directory tree which is being served over HTTP.
I have a project on GitHub that has a directory containing some automatically generated
I have a ListCtrl containing some items representing (huge and remote) files. I'd like

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.