Library contents

Python Path.chmod

Set and inspect POSIX file permission bits.

A simple example

Path.chmod

  1. Start with Owner-only permission to read and write
  2. Do this Inspect the permission bits
  3. Result Owner can read and write
On a POSIX filesystem the owner read and write bits correspond to mode 0600 when all other bits are clear.

Syntax

Python 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.py

Limit 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.

Resultprivate
lesson.py
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")
Output
private

Only the owner read and write bits are set.

Advertisements

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?

GivenFile contents “clay”
Worked result600

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?

GivenFile contents “ring bell”
Worked result600

What to watch for

The example requires POSIX permission support. Mode bits alone do not describe every access control rule.

Documentation for Path.chmod