Skip to content

plistlib

The Python plistlib module provides an interface for reading and writing the property list files used by Apple, primarily on macOS and iOS. It supports both the XML and binary .plist formats and maps plist values to native Python built-in data types, such as dictionaries, lists, strings, integers, floats, booleans, bytes, and datetime.datetime objects.

Here’s a quick example that serializes a dictionary to an XML plist and reads it back:

Language: Python
>>> import plistlib

>>> data = {"name": "Alice", "age": 30, "pets": ["cat", "dog"]}
>>> xml = plistlib.dumps(data)
>>> plistlib.loads(xml)
{'age': 30, 'name': 'Alice', 'pets': ['cat', 'dog']}

Key Features

  • Reads and writes XML and binary plist formats, selected with the FMT_XML and FMT_BINARY constants
  • Autodetects the file format when loading
  • Maps plist values to native Python types and vice versa
  • Sorts dictionary keys on output by default for reproducible files
  • Supports timezone-aware datetime objects through the aware_datetime flag
  • Handles UID tokens produced by NSKeyedArchiver and NSKeyedUnarchiver

Frequently Used Classes and Functions

Object Type Description
plistlib.load() Function Reads a plist from a binary file object
plistlib.loads() Function Parses a plist from a bytes or str object
plistlib.dump() Function Writes a Python value to a binary file as a plist
plistlib.dumps() Function Returns a Python value serialized as plist bytes
plistlib.UID Class Wraps an integer UID used in NSKeyedArchiver data
plistlib.FMT_XML Constant Selects the XML plist format
plistlib.FMT_BINARY Constant Selects the binary plist format
plistlib.InvalidFileException Exception Raised when a file cannot be parsed as a plist

Examples

Write a dictionary to a binary plist file:

Language: Python
>>> import plistlib

>>> data = {"theme": "dark", "version": 3}
>>> with open("prefs.plist", "wb") as fp:
...     plistlib.dump(data, fp, fmt=plistlib.FMT_BINARY)
...

Read the same file back and let plistlib autodetect the format:

Language: Python
>>> import plistlib

>>> with open("prefs.plist", "rb") as fp:
...     plistlib.load(fp)
...
{'theme': 'dark', 'version': 3}

Parse a plist literal stored in a bytes object:

Language: Python
>>> import plistlib

>>> raw = b"""<?xml version="1.0" encoding="UTF-8"?>
... <plist version="1.0">
... <dict>
...   <key>CFBundleName</key>
...   <string>Calculator</string>
...   <key>CFBundleVersion</key>
...   <string>1.0.0</string>
... </dict>
... </plist>"""
>>> plistlib.loads(raw)
{'CFBundleName': 'Calculator', 'CFBundleVersion': '1.0.0'}

Common Use Cases

The most common tasks for plistlib include:

  • Reading application preferences and settings on macOS and iOS
  • Inspecting Info.plist metadata inside macOS application bundles
  • Converting between XML and binary plist formats
  • Generating configuration files consumed by Apple frameworks and tools
  • Extracting data from NSKeyedArchiver archives for debugging or analysis

Real-World Example

Consider a small script that reads the bundle identifier from an Info.plist file inside a macOS application bundle:

Language: Python Filename: read_bundle_id.py
import plistlib
from pathlib import Path

def bundle_identifier(app_path):
    info_plist = Path(app_path) / "Contents" / "Info.plist"
    with info_plist.open("rb") as fp:
        info = plistlib.load(fp)
    return info.get("CFBundleIdentifier", "unknown")

if __name__ == "__main__":
    print(bundle_identifier("/Applications/Safari.app"))

Run it on the command line:

Language: Shell
$ python read_bundle_id.py
com.apple.Safari

The plistlib.load() function transparently handles both XML and binary plists, so the same code works regardless of how the Info.plist file was originally saved.

Tutorial

Working With JSON Data in Python

Learn how to work with JSON data in Python using the json module. Convert, read, write, and validate JSON files and handle JSON data for APIs and storage.

intermediate python stdlib

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated May 8, 2026