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:
>>> 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_XMLandFMT_BINARYconstants - 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
datetimeobjects through theaware_datetimeflag - Handles
UIDtokens produced byNSKeyedArchiverandNSKeyedUnarchiver
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:
>>> 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:
>>> 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:
>>> 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.plistmetadata inside macOS application bundles - Converting between XML and binary plist formats
- Generating configuration files consumed by Apple frameworks and tools
- Extracting data from
NSKeyedArchiverarchives 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:
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:
$ 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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Python and TOML: Read, Write, and Configure with tomllib (Tutorial)
- The Python pickle Module: How to Persist Objects in Python (Tutorial)
- Serialize Your Data With Python (Tutorial)
- YAML: The Missing Battery in Python (Tutorial)
- A Roadmap to XML Parsers in Python (Tutorial)
- Python's pathlib Module: Taming the File System (Tutorial)
- Working With JSON in Python (Course)
- Working With JSON Data in Python (Quiz)
- Working With TOML and Python (Course)
- Serializing Objects With the Python pickle Module (Course)
- YAML: Python's Missing Battery (Course)
- Using Python's pathlib Module (Course)
- Python's pathlib Module: Taming the File System (Quiz)
By Leodanis Pozo Ramos • Updated May 8, 2026