GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [doc] os.walk() consider some symlinks as dirs instead of non-dirs · Issue #57179 · python/cpython

[doc] os.walk() consider some symlinks as dirs instead of non-dirs #57179

New issue
Copy link
New issue
Copy link
Closed
Closed
[doc] os.walk() consider some symlinks as dirs instead of non-dirs#57179
Copy link
Assignees
JelleZijlstra
Labels
3.11only security fixesonly security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirtype-featureA feature request or enhancementA feature request or enhancement

Description

socketpair
mannequin
opened on Sep 13, 2011
Issue body actions
BPO 12970
Nosy @ncoghlan, @vstinner, @benhoyt, @4kir4, @socketpair
Files
  • z.patch: patch for the problem
  • docs-walk-issue12970.patch: Update os.walk() docs
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2011-09-13.13:02:11.368>
    labels = ['3.11', 'type-feature', 'docs']
    title = '[doc] os.walk() consider some symlinks as dirs instead of non-dirs'
    updated_at = <Date 2021-11-28.13:04:55.425>
    user = 'https://github.com/socketpair'

    bugs.python.org fields:

    activity = <Date 2021-11-28.13:04:55.425>
    actor = 'iritkatriel'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2011-09-13.13:02:11.368>
    creator = 'socketpair'
    dependencies = []
    files = ['23140', '36138']
    hgrepos = []
    issue_num = 12970
    keywords = ['patch']
    message_count = 5.0
    messages = ['143965', '143967', '152805', '223367', '224167']
    nosy_count = 9.0
    nosy_names = ['ncoghlan', 'vstinner', 'benhoyt', 'docs@python', 'akira', 'socketpair', 'alexey-smirnov', 'Sung-Yu.Chen', 'ukl']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue12970'
    versions = ['Python 3.11']

    Activity

    1. socketpair commented on Sep 13, 2011

      @socketpair
      socketpairmannequin
      on Sep 13, 2011
      MannequinAuthor
      More actions

      Consider code:

      for (root, dirs, nondirs) in os.walk(path, followlinks=False):
          print (nondirs)

      This code will not print symlinks that refer to some dir. I think it is the bug.

      In other words: If followlinks is True, we should consider some symlinks as dirs. If not, any symlink is the non-dir.

      Patch included.

      Also, please fix documentation about this nuance.

    2. socketpair
      added
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      on Sep 13, 2011
    3. socketpair
      assigned
      docspython
      on Sep 13, 2011
    4. socketpair
      added
      docsDocumentation in the Doc dirDocumentation in the Doc dir
      on Sep 13, 2011
    5. socketpair commented on Sep 13, 2011

      @socketpair
      socketpairmannequin
      on Sep 13, 2011
      MannequinAuthor
      More actions

      Also, there is some mis-optimisation for followlinks=False: stat() and then lstat() will be called. Instead of one lstat().

      Code may be rewritten as (but I don't know about cross-platform issues):
      ---------------------------------

      if followlinks:
          mode = os.stat(path).st_mode
      else:
          mode = os.lstat(path).st_mode
      
      if stat.S_ISDIR(mode):
          dirs.append(path)
      else:
          nondir.append(path)

      It will be much cleaner than current (or patched with my patch) implementation

    6. socketpair
      changed the title [-]os.wlak() consider some symlinks as dirs instead of non-dirs[/-] [+]os.walk() consider some symlinks as dirs instead of non-dirs[/+] on Sep 13, 2011
    7. ncoghlan commented on Feb 7, 2012

      @ncoghlan
      ncoghlan
      on Feb 7, 2012
      Contributor
      More actions

      This behaviour came up recently when implementing os.fwalk() [1]. There are problems with all 3 possible approaches (list as dirs, list as files, don't list at all) when followlinks is False. Since all alternatives are potentially surprising, the current behaviour wins by default (as people will already have written their code to cope with that behaviour and there's no net gain in changing the default, since the desired treatment of such links will vary according to the task at hand).

      As a result, I'm converting this to a pure documentation issue - the os.walk() docs should definitely mention this subtlety. The behaviour won't be changing, though.

      [1] http://bugs.python.org/issue13734,#msg151077

    8. ncoghlan
      added
      type-featureA feature request or enhancementA feature request or enhancement
      and removed
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      on Feb 7, 2012
    9. ukl commented on Jul 17, 2014

      @ukl
      uklmannequin
      on Jul 17, 2014
      Mannequin
      More actions

      I like the function as it is documented, i.e. "filenames is a list of the names of the non-directory files in dirpath.". This includes all symlinks (in the followlinks=False cast at least).

      I'd say not including symlinks to directories but symlinks to files is a magnitude more surprising than treating a symlink to a directory as a file. And if you consider this as a short comming of the documentation this isn't (IMHO) a subtlety. The (my?) intuition says: all entries of a root (apart from . and .. as documented) are included in either dirnames or filenames.

      Yes, changing behaviour here might break some code, but this applies to all changes.

      For some usecases it might be right to just skip over symlinks-to-dirs, but if it's not you have to opendir + read all root entries again in the loop to find all symlinks which effectively means reimplementing os.walk.

    10. 4kir4 commented on Jul 28, 2014

      @4kir4
      4kir4mannequin
      on Jul 28, 2014
      Mannequin
      More actions

      I've updated os.walk() documentation to mention that *dirnames* list
      includes symlinks to directories.

      To imitate the other two cases:

      • treat the symlinks as files:
          for dirpath, dirnames, files in os.walk(top):
              dirs = []
              for name in dirnames:
                  (files if islink(join(dirpath, name)) else dirs).append(name)
              dirnames = dirs
      • don't include in either of the lists:
          for dirpath, dirnames, files in os.walk(top):
              dirnames[:] = [name for name in dirnames
                             if not islink(join(dirpath, name))]

      where islink = os.path.islink and join = os.path.join.

      I've uploaded the documentation patch. Please, review.

    11. iritkatriel
      added
      3.11only security fixesonly security fixes
      on Nov 28, 2021
    12. iritkatriel
      changed the title [-]os.walk() consider some symlinks as dirs instead of non-dirs[/-] [+][doc] os.walk() consider some symlinks as dirs instead of non-dirs[/+] on Nov 28, 2021
    13. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    14. JelleZijlstra
      added a commit that references this issue on Oct 7, 2022

      gh-57179: Add note on symlinks for os.walk (#94799)

      0f498f1
    15. JelleZijlstra
      self-assigned this
      on Oct 7, 2022
    16. miss-islington
      added 4 commits that reference this issue on Oct 7, 2022

      pythongh-57179: Add note on symlinks for os.walk (pythonGH-94799)

      0322ccc

      pythongh-57179: Add note on symlinks for os.walk (pythonGH-94799)

      411cd33

      gh-57179: Add note on symlinks for os.walk (GH-94799)

      8d6591b

      gh-57179: Add note on symlinks for os.walk (GH-94799)

      1818235
    17. slateny
      closed this as completedon Oct 8, 2022
    18. carljm
      added 2 commits that reference this issue on Oct 8, 2022

      Merge branch 'main' into typewatch

      27a4980

      Merge branch 'main' into urllibnonet

      132c32e
    19. mpage
      added a commit that references this issue on Oct 11, 2022

      pythongh-57179: Add note on symlinks for os.walk (python#94799)

      c42b931
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    • @JelleZijlstra
      JelleZijlstra

    Labels

    3.11only security fixesonly security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.