Generate and parse Mac OS X .plist files using Python (plistlib)

PythonServer Side ProgrammingProgramming

Files with '.plist' the extension is used by Mac OS X applications to store application properties. The plislib module provides an interface to read/write operations of these property list files.

The plist file format serializes basic object types, like dictionaries, lists, numbers, and strings. Usually, the top level object is a dictionary. To write out and to parse a plist file, use the dump() and load() functions. Serialized byte strings are handled by use dumps() and loads() functions. Values can be strings, integers, floats, booleans, tuples, lists, dictionaries (but only with string keys).

This module defines the following functions −

load()Read a plist file pointed by readable and binary file object. format of the file and the following values are valid
  • None − Autodetect the file format

  • FMT_XML − XML file format

  • FMT_BINARY − Binary plist format

dump()Write value to a plist file refereed by writable, binary file object. The fmt argument specifies the format of the plist file and can be one of the following values
  • FMT_XML − XML formatted plist file

  • FMT_BINARY − Binary formatted plist file

loads()Load a plist from a bytes object. See load() for an explanation of the keyword arguments.
dumps()Return value as a plist-formatted bytes object. See the documentation for dump() for an explanation of the keyword arguments of this function.

Following script stores serialized dictionary in plist file

import plistlib
properties = {
   "name" : "Ramesh",
   "College":"ABC College",
   "marks" : {"phy":60, "che":60, "maths":60}
plistlib.dump(pl, fileName)

To read plist file use load() function

with open('marks.plist', 'rb') as fp:
   pl = plistlib.load(fp)
Updated on 30-Jul-2019 22:30:25