-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pathlib.chown method #64978
Comments
For pragmatic and philosophical reasons, I would argue that we should add chown to pathlib library. |
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? |
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. |
+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 |
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 |
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 This seems to address the major concrete issue expressed by users here (e.g. "It's annoying to have to drop back to (Sidenote: Perhaps, if there's interest, it's worth allowing passing through |
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 |
It will be super cool to have this method because there is awesome python-powered xonsh shell and Path objects are using in xonsh broadly. You can do this in the shell using path-strings: pip install -U 'xonsh[full]'
xonsh
cd /tmp && echo world > hello.txt
p'hello.txt'
# Path('hello')
p'hello.txt'.read_text()
# 'world\n'
for f in p'/tmp'.glob('hello*'):
print(f, f.exists())
# /tmp/hello.txt True It will be cool to run I'm for reopening this issue and favor for enriching pathlib. |
Re-opening the issue for the sake of discussion. I'm ±0 on this idea. On the one hand, I don't want pathlib to accumulate simple wrappers of On the other hand, the implementation of Would love to hear thoughts from others! |
I prefer |
Personally, FWIW, I'm +0.5 so long as the idea fits with the long-term plan and the implementation does not add more than minimal additional maintenance burden. It's a little unfortunate there are two different |
Thanks both. IMO we should move the implementation from # shutil.py
def chown(path, user=None, group=None):
pathlib.Path(path).chown(user, group) However, we'd need to add support for |
FYI I disagree with that as we want people to move towards string-based paths as they are much easier to work with (just like we want people to use UTF-8 more). |
Hm. I wouldn't want to encourage users to use But POSIX doesn't specify a particular character encoding for paths, and portable POSIX applications can't assume one. Pathnames are byte strings that may be in no particular encoding. If we can find a way to support this in pathlib without compromising the usability for people who don't care, I'd like to pursue it. Maybe. |
I view pathlib refusing to handle bytes as a feature. If I have a function that accepts an arbitrary Even if we can add support for bytes paths without complicating the implementation or slowing things down, I think I'd still be against adding support for bytes paths in pathlib 😕 it would raise backwards-compatibility concerns and increase the cognitive load associated with the module, and I'm unsure that there's a strong use case for it. |
I find that quite convincing! Thanks. It sounds like we can't implement |
If users were allowed to pass |
I don't follow, sorry - why would they have to be converted to |
(I mainly use Windows so apologies if this question exposes my ignorance about bytes paths!) Are there any drawbacks to the solution given in the answer to that Stack Overflow question? It looks pretty "clean" to me — if there are no drawbacks, maybe we should just document in the pathlib docs that this is how you can use pathlib if you have a bytes path? |
I think what I'm missing is a proper understanding of |
PEP 383 is the fundamental reference here. |
Because pathlib uses |
Move implementation from `shutil.chown()`. This function now calls through to pathlib.
Move implementation from `shutil.chown()`. This function now calls through to pathlib.
Move implementation from `shutil.chown()`. This function now calls through to pathlib.
PR available: |
Barney, what's the status here? |
I'm still undecided as to whether this is a good idea. IIUC we can't make Consequently we'd either need to make |
#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 |
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()
#104183The text was updated successfully, but these errors were encountered: