Skip to content

Add pathlib.chown method #64978

Description

@vajrasky
mannequin
BPO 20779
Nosy @pitrou, @vajrasky, @sedrubal, @eamanu, @y0urself
PRs
  • gh-64978: Add chown() to pathlib.Path #31212
  • Files
  • add_chown_to_pathlib.patch
  • 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 2014-02-26.09:51:50.192>
    labels = ['type-feature', 'library', '3.11']
    title = 'Add pathlib.chown method'
    updated_at = <Date 2022-02-08.11:17:25.947>
    user = 'https://github.com/vajrasky'

    bugs.python.org fields:

    activity = <Date 2022-02-08.11:17:25.947>
    actor = 'y0urself'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2014-02-26.09:51:50.192>
    creator = 'vajrasky'
    dependencies = []
    files = ['34227']
    hgrepos = []
    issue_num = 20779
    keywords = ['patch']
    message_count = 7.0
    messages = ['212245', '212378', '212411', '212425', '396629', '398695', '412787']
    nosy_count = 8.0
    nosy_names = ['pitrou', 'cvrebert', 'neologix', 'vajrasky', 'sedrubal', 'eamanu', 'Tom Cook', 'y0urself']
    pr_nums = ['31212']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue20779'
    versions = ['Python 3.11']

    Linked PRs

    Activity

    1. vajrasky commented on Feb 26, 2014

      vajraskymannequin
      MannequinAuthor

      For pragmatic and philosophical reasons, I would argue that we should add chown to pathlib library.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Feb 26, 2014
    3. pitrou commented on Feb 27, 2014

      @pitrou
      Member

      I don't know about philosophical reasons ;-), but I don't think chown is very often used. Do you know of a context where it is?

    4. vajrasky commented on Feb 28, 2014

      vajraskymannequin
      MannequinAuthor

      Pragmatic reasons:

      I use chown moderately often. Usually as root, I want to change the uid of the files after generating a bunch of files. But I almost never changed gid.

      I am in two mind whether I should add lchown as well or not (just like lchmod) and make the signature of pathlib.chown same as os.chown (with third parameter, follow_symbolic_links).

      If you close this as won't fix, I will not hold grudge against you, though. ;)

    5. pitrou commented on Feb 28, 2014

      @pitrou
      Member

      Well, I don't know yet, but if we ever want chown() in pathlib, I think it should be higher-level and accept symbolic uids as well.

    6. TomCook commented on Jun 28, 2021

      TomCookmannequin
      Mannequin

      +1 this.

      I have a program that opens a UNIX socket as root for other processes to communicate with it. I need to set the permissions to 0o775 and set the owner gid to a specific group so that members of that group can communicate with the process. It's annoying to have to drop back to os.chown(str(path), ...) for this.

    7. eamanu commented on Aug 1, 2021

      eamanumannequin
      Mannequin

      Hello!

      I also use it frequently, but looking this issue I can agree with pitrou that there aren't lot of people that use chown.

      If I'm not wrong, there's a trend to move os to pathlib, perhaps chown should be in pathlib too :)

      I can work on this.

    8. y0urself commented on Feb 7, 2022

      y0urselfmannequin
      Mannequin

      I would love to use chown for a pathlib Path, too.

      It may not be used often, but if anyone want's to change all the os references from a file, it would be cool to have it on pathlib!

    9. transferred this issue fromon Apr 10, 2022
    10. barneygale commented on Jan 8, 2023

      @barneygale
      Contributor

      I'm -1 on this: I don't think pathlib should start accumulating further functionality from os unless its used to implement high-level path behaviour (like walking directory trees).

    11. CAM-Gerlach commented on Jan 8, 2023

      @CAM-Gerlach
      Member

      To note, I can't confirm myself since I'm on a Windows machine at the moment, but os.chown is documented as natively supporting pathlib.Paths since Python 3.6. There's also the higher level shutil.chown which has a nicer API with more Pythonic argument handling and accepts user/group names as well as IDs, unlike the os version. As it simply passes path through to the underlying os.chown, it in turn supports pathlib.Paths as well.

      This seems to address the major concrete issue expressed by users here (e.g. "It's annoying to have to drop back to os.chown(str(path), ...)", "I would love to use chown for a pathlib Path, too."), as well as exposing a nicer higher-level interface without the need to add yet another chown in pathlib.

      (Sidenote: Perhaps, if there's interest, it's worth allowing passing through dir_fd or particularly follow_symlinks in shutil.chown to os.chown so the latter is a proper superset of the former, but that's an entirely separate issue of course and unrelated to pathlib.)

    12. AlexWaygood commented on Jan 8, 2023

      @AlexWaygood
      Member

      I agree with @barneygale, @CAM-Gerlach, and @brettcannon (Brett voiced his opinions on the PR here: #31212 (comment)). There seems to be little point adding this method, if it's just a thin wrapper around os.chown().

    13. 24 remaining items

    14. barneygale commented on May 4, 2023

      @barneygale
      Contributor
    15. added a commit that references this issue on May 7, 2023
    16. added
      3.13only security fixes
      and removed
      3.12only security fixes
      on Jan 5, 2024
    17. added
      3.14bugs and security fixes
      and removed
      3.13only security fixes
      on May 8, 2024
    18. removed their assignment
      on May 11, 2024
    19. nineteendo commented on Jun 2, 2024

      @nineteendo
      Contributor

      Barney, what's the status here?

    20. barneygale commented on Jun 2, 2024

      @barneygale
      Contributor

      I'm still undecided as to whether this is a good idea.

      IIUC we can't make shutil use pathlib because pathlib strips trailing slashes, and so shutil.chown('not_a_dir/', ...) would succeed where previously it failed with NotADirectoryError. This is fixable if we introduce a private superclass of Path that skips normalization, but there's little appetite for that right now.

      Consequently we'd either need to make pathlib use shutil, which feels wrong to some core devs and I'm trying to avoid if I can, or we'd just copy-paste the shutil implementation into pathlib, which I feel pretty "meh" about too.

    21. barneygale commented on Jun 2, 2024

      @barneygale
      Contributor

      #73991 is the more important shutil/pathlib issue. I hope to solve that first, and by doing so, shed some light on whether this request is worth doing or not.

    22. barneygale commented on Jul 3, 2024

      @barneygale
      Contributor

      I'm going to close this as "won't do". It turns out that Path.chown() won't be helpful for copying file metadata in #73991, and I can see no other compelling reason to add it. Plus it's POSIX-only and usually needs root. Use os.chown() or shutil.chown() instead please! And feel free to leave a comment if you disagree and would like to see this re-opened.

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

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-pathlibtype-featureA feature request or enhancement

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions