plistlib — Generate and parse Apple.plist files

Source code:Lib/plistlib.py


This module provides an interface for reading and writing the “property list”files used by Apple, primarily on macOS and iOS. This module supports both binaryand XML plist files.

The property list (.plist) file format is a simple serialization supportingbasic object types, like dictionaries, lists, numbers and strings. Usually thetop level object is a dictionary.

To write out and to parse a plist file, use thedump() andload() functions.

To work with plist data in bytes or string objects, usedumps()andloads().

Values can be strings, integers, floats, booleans, tuples, lists, dictionaries(but only with string keys),bytes,bytearrayordatetime.datetime objects.

Changed in version 3.4:New API, old API deprecated. Support for binary format plists added.

Changed in version 3.8:Support added for reading and writingUID tokens in binary plists as usedby NSKeyedArchiver and NSKeyedUnarchiver.

Changed in version 3.9:Old API removed.

See also

PList manual page

Apple’s documentation of the file format.

This module defines the following functions:

plistlib.load(fp,*,fmt=None,dict_type=dict,aware_datetime=False)

Read a plist file.fp should be a readable and binary file object.Return the unpacked root object (which usually is adictionary).

Thefmt is the format of the file and the following values are valid:

Thedict_type is the type used for dictionaries that are read from theplist file.

Whenaware_datetime is true, fields with typedatetime.datetime willbe created asaware object, withtzinfo asdatetime.UTC.

XML data for theFMT_XML format is parsed using the Expat parserfromxml.parsers.expat – see its documentation for possibleexceptions on ill-formed XML. Unknown elements will simply be ignoredby the plist parser.

The parser raisesInvalidFileException when the file cannot be parsed.

Added in version 3.4.

Changed in version 3.13:The keyword-only parameteraware_datetime has been added.

plistlib.loads(data,*,fmt=None,dict_type=dict,aware_datetime=False)

Load a plist from a bytes or string object. Seeload() for anexplanation of the keyword arguments.

Added in version 3.4.

Changed in version 3.13:data can be a string whenfmt equalsFMT_XML.

plistlib.dump(value,fp,*,fmt=FMT_XML,sort_keys=True,skipkeys=False,aware_datetime=False)

Writevalue to a plist file.fp should be a writable, binaryfile object.

Thefmt argument specifies the format of the plist file and can beone of the following values:

Whensort_keys is true (the default) the keys for dictionaries will bewritten to the plist in sorted order, otherwise they will be written inthe iteration order of the dictionary.

Whenskipkeys is false (the default) the function raisesTypeErrorwhen a key of a dictionary is not a string, otherwise such keys are skipped.

Whenaware_datetime is true and any field with typedatetime.datetimeis set as anaware object, it will convert toUTC timezone before writing it.

ATypeError will be raised if the object is of an unsupported type ora container that contains objects of unsupported types.

AnOverflowError will be raised for integer values that cannotbe represented in (binary) plist files.

Added in version 3.4.

Changed in version 3.13:The keyword-only parameteraware_datetime has been added.

plistlib.dumps(value,*,fmt=FMT_XML,sort_keys=True,skipkeys=False,aware_datetime=False)

Returnvalue as a plist-formatted bytes object. Seethe documentation fordump() for an explanation of the keywordarguments of this function.

Added in version 3.4.

The following classes are available:

classplistlib.UID(data)

Wraps anint. This is used when reading or writing NSKeyedArchiverencoded data, which contains UID (see PList manual).

data

Int value of the UID. It must be in the range0<=data<2**64.

Added in version 3.8.

The following constants are available:

plistlib.FMT_XML

The XML format for plist files.

Added in version 3.4.

plistlib.FMT_BINARY

The binary format for plist files

Added in version 3.4.

The module defines the following exceptions:

exceptionplistlib.InvalidFileException

Raised when a file cannot be parsed.

Added in version 3.4.

Examples

Generating a plist:

importdatetimeimportplistlibpl=dict(aString="Doodah",aList=["A","B",12,32.1,[1,2,3]],aFloat=0.1,anInt=728,aDict=dict(anotherString="<hello & hi there!>",aThirdString="M\xe4ssig, Ma\xdf",aTrueValue=True,aFalseValue=False,),someData=b"<binary gunk>",someMoreData=b"<lots of binary gunk>"*10,aDate=datetime.datetime.now())print(plistlib.dumps(pl).decode())

Parsing a plist:

importplistlibplist=b"""<plist version="1.0"><dict>    <key>foo</key>    <string>bar</string></dict></plist>"""pl=plistlib.loads(plist)print(pl["foo"])