How do I get the OS System Font Directory(Cross-Platform) in python?

Chris “Kwpolska” Warrick kwpolska at
Thu Jul 11 18:12:43 CEST 2013

On Thu, Jul 11, 2013 at 08:54:17AM -0700, Metallicow wrote:
> For a portable font install tool.
> Finding if a particular font exists,
> useful when testing apps in virtual environent,
> rendering text from a font,
> Font file manipulations,
> etc..
> -- 

Then do a nice little `if` checking the user’s OS. hints what to use,
and you need to do:

if sys.platform.startswith('win') or sys.platform.startswith('cygwin'):
    FONTDIRS = [os.path.join(os.environ['WINDIR'], 'Fonts')
elif sys.platform.startswith('darwin'):
    # [see below and devise it yourself]
else: # linux, *bsd and everything else
    # [see below, commit suicide and develop this bit]

    The easiest of those three, all the fonts are in %WINDIR%/Fonts.
    In Python: os.path.join(os.environ['WINDIR'], 'Fonts')

Mac OS X: (and not all of them are
    guaranteed to exist).  `os.expanduser()` may be useful for
    that first one.

Linux, and everything else running that fancy open stuff (eg. *BSD):
    Hardcore.  You just need to parse a nice tiny 155-line XML file.

    It’s /etc/fonts/fonts.conf, and it has some <dir> tags.
    Some of them may have a `prefix="xdg"` attribute which makes you
    prepend the value with $XDG_DATA_HOME (~/.local/share if that is
    empty).  You also need `os.expanduser()`.

    Oh: and you need to go recursively through them, as subdirectories
    are also checked for fonts (and it probably goes further)

Kwpolska <> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post        |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list