Repository navigation
Add pathlib.chown method #64978
Description
Activity
For pragmatic and philosophical reasons, I would argue that we should add chown to pathlib library.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
on Feb 26, 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?
Reacted by Miroslav Cibulka and Leonard PeilPragmatic 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.
+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.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.
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!
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).Reacted by C.A.M. Gerlach and Ethan KnoxTo 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.)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
PR available:
- added a commit that references this issue
on May 7, 2023 - added3.13only security fixesonly security fixesand removed3.12only security fixesonly security fixes
on Jan 5, 2024 - added3.14bugs and security fixesbugs and security fixesand removed3.13only security fixesonly security fixes
on May 8, 2024 Barney, what's the status here?
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.Reacted by Nice Zombies#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. Useos.chown()orshutil.chown()instead please! And feel free to leave a comment if you disagree and would like to see this re-opened.Reacted by Sheeva Plug
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
chown()topathlib.Path#31212Note: 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:
bugs.python.org fields:
Linked PRs
pathlib.Path.chown()#104183