Python Path.chmod
Set and inspect POSIX file permission bits.
A simple example
Path.chmod
- Start with Owner-only permission to read and write
- Do this Inspect the permission bits
- Result Owner can read and write
Syntax
payroll.chmod(0o600)
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyLimit a practice payroll file to its owner
A payroll assistant is preparing a local export on a POSIX volume. Set owner-only permissions on a disposable file and verify its mode before real employee data would be written.
from pathlib import Path
from tempfile import TemporaryDirectory
import stat
with TemporaryDirectory() as folder:
base = Path(folder)
payroll = base / "payroll"
payroll.touch()
payroll.chmod(0o600)
if stat.S_IMODE(payroll.stat().st_mode) == 0o600:
print("private")
private
Only the owner read and write bits are set.
Prepare a gallery label
A curator stores a short UTF-8 caption beside an exhibit. On a POSIX filesystem the owner sets read and write access for the owner alone. What are the resulting permission bits in octal?
Prepare a studio handover
A stage manager saves a two-word instruction for the next shift. On a POSIX filesystem the owner sets read and write access for the owner alone. What are the resulting permission bits in octal?
What to watch for
The example requires POSIX permission support. Mode bits alone do not describe every access control rule.