On this page
os.chown
Prev: os.chmod | Up: os | Next: os.close
Rel: os.abort, os.access, os.chdir, os.chmod
Ref: docs.python.org — os.chown
Tags: #public #python3 #stdlib #os
Import
import os
from os import chown
Signature & Overview
os.chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)
Change the owner and group id of path to the numeric uid and gid.
Usage
import os
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "f.txt"
p.write_text("ok", encoding="utf-8")
if hasattr(os, "getuid"):
os.chown(p, os.getuid(), os.getgid())
Gotchas
- Unix-only and numeric IDs only: Pass
-1to leave eitheruidorgidunchanged, or useshutil.chown()if you have username/group strings.
REPL Inspect
from os import chown
help(chown)
Linked from
Related notes
- tuts/python3/stdlib_module/os/abort Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/access Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chdir Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chmod Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/close Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/os/cpu_count shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/DirEntry shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/dup shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/dup2 shared tags: #os, #stdlib