You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
Reacted by Miroslav Cibulka and Leonard Peil
vajrasky commented on Feb 28, 2014
vajraskymannequin
MannequinAuthor
More actions
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. ;)
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.
TomCook commented on Jun 28, 2021
TomCookmannequin
Mannequin
More actions
+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.
eamanu commented on Aug 1, 2021
eamanumannequin
Mannequin
More actions
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'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).
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.)
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().
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.
#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.
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.
Activity
vajrasky commentedon Feb 26, 2014
For pragmatic and philosophical reasons, I would argue that we should add chown to pathlib library.
pitrou commentedon Feb 27, 2014
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?
vajrasky commentedon Feb 28, 2014
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. ;)
pitrou commentedon Feb 28, 2014
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.
TomCook commentedon Jun 28, 2021
+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.eamanu commentedon Aug 1, 2021
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.
y0urself commentedon Feb 7, 2022
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!
barneygale commentedon Jan 8, 2023
I'm -1 on this: I don't think pathlib should start accumulating further functionality from
osunless its used to implement high-level path behaviour (like walking directory trees).CAM-Gerlach commentedon Jan 8, 2023
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 levelshutil.chownwhich has a nicer API with more Pythonic argument handling and accepts user/group names as well as IDs, unlike theosversion. As it simply passespaththrough to the underlyingos.chown, it in turn supportspathlib.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 anotherchowninpathlib.(Sidenote: Perhaps, if there's interest, it's worth allowing passing through
dir_fdor particularlyfollow_symlinksinshutil.chowntoos.chownso the latter is a proper superset of the former, but that's an entirely separate issue of course and unrelated topathlib.)AlexWaygood commentedon Jan 8, 2023
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().24 remaining items
barneygale commentedon May 4, 2023
PR available:
pathlib.Path.chown()#104183Merge branch 'main' into pythongh-64978-add-pathlib-path-chown
nineteendo commentedon Jun 2, 2024
Barney, what's the status here?
barneygale commentedon Jun 2, 2024
I'm still undecided as to whether this is a good idea.
IIUC we can't make
shutilusepathlibbecause pathlib strips trailing slashes, and soshutil.chown('not_a_dir/', ...)would succeed where previously it failed withNotADirectoryError. This is fixable if we introduce a private superclass ofPaththat skips normalization, but there's little appetite for that right now.Consequently we'd either need to make
pathlibuseshutil, 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.barneygale commentedon Jun 2, 2024
#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.
barneygale commentedon Jul 3, 2024
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. Useos.chown()orshutil.chown()instead please! And feel free to leave a comment if you disagree and would like to see this re-opened.