On this page
plistlib
Prev: platform | Up: stdlib_module | Next: poplib
Rel: csv, configparser, tomllib, netrc
Ref: docs.python.org — plistlib
Tags: #public #python3 #stdlib
Import
import plistlib
Overview
Reads and writes Apple Property List (.plist) files in both XML (FMT_XML) and binary (FMT_BINARY) formats (loads, dumps, load, dump). Essential for macOS automation—inspecting Info.plist app bundles, LaunchAgents definitions, and macOS preferences cleanly with zero external dependencies.
Quick Start
import plistlib
data = {"CFBundleIdentifier": "com.wispsy.demo", "LSUIElement": True, "Version": 1}
xml_bytes = plistlib.dumps(data, fmt=plistlib.FMT_XML)
assert plistlib.loads(xml_bytes) == data
bin_bytes = plistlib.dumps(data, fmt=plistlib.FMT_BINARY)
assert plistlib.loads(bin_bytes) == data
Implementation Walkthrough
1. Reading & Updating a macOS .plist File Safely
Both load() and dump() require binary file handles ("rb" / "wb"):
from pathlib import Path
import plistlib
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "com.example.agent.plist"
config = {"Label": "com.example.agent", "ProgramArguments": ["/usr/bin/true"]}
with p.open("wb") as fh:
plistlib.dump(config, fh, fmt=plistlib.FMT_XML)
with p.open("rb") as fh:
loaded = plistlib.load(fh)
assert loaded["Label"] == "com.example.agent"
Gotchas & Boundaries
- Always open files in binary mode (
"rb"/"wb"):plistlibrejects text streams. - XML plists cannot serialize
None: Onlydict(string keys),list,str,int,float,bool,bytes, anddatetimeare supported. - Binary plists are compact and fast; XML plists are git-diff-friendly—
plistlib.load()auto-detects both.
Common Functions
- plistlib.dump() —
dump(value, fp, *, fmt=<PlistFormat.FMT_XML: 1>, sort_keys=True, skipkeys=False, aware_datetime=False)— Write 'value' to a .plist file. 'fp' should be a writable, binary file object. - plistlib.dumps() —
dumps(value, *, fmt=<PlistFormat.FMT_XML: 1>, skipkeys=False, sort_keys=True, aware_datetime=False)— Return a bytes object with the contents for a .plist file. - plistlib.load() —
load(fp, *, fmt=None, dict_type=<class 'dict'>, aware_datetime=False)— Read a .plist file. 'fp' should be a readable and binary file object. - plistlib.loads() —
loads(value, *, fmt=None, dict_type=<class 'dict'>, aware_datetime=False)— Read a .plist file from a bytes object.
Classes & Objects
- plistlib.InvalidFileException —
plistlib.InvalidFileExceptionclass. (Methods:.add_note(),.with_traceback()) - plistlib.UID —
plistlib.UIDclass.
Constants & Attributes
plistlib.FMT_BINARY(PlistFormat) =<PlistFormat.FMT_BINARY: 2>plistlib.FMT_XML(PlistFormat) =<PlistFormat.FMT_XML: 1>
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/configparser
- tuts/python3/stdlib_module/csv
- tuts/python3/stdlib_module/netrc
- tuts/python3/stdlib_module/platform
- tuts/python3/stdlib_module/plistlib/dump
- tuts/python3/stdlib_module/plistlib/dumps
- tuts/python3/stdlib_module/plistlib/InvalidFileException
- tuts/python3/stdlib_module/plistlib/load
- tuts/python3/stdlib_module/plistlib/loads
- tuts/python3/stdlib_module/plistlib/UID
- tuts/python3/stdlib_module/poplib
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tomllib
Related notes
- tuts/python3/stdlib_module/configparser Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/csv Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/netrc Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/platform Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/plistlib/dump Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/plistlib/dumps Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/plistlib/InvalidFileException Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/plistlib/load Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/plistlib/loads Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/plistlib/UID Linked here; links to this page; shared tags: #stdlib