Counting Keys

Counting the number of keys in your public keybox (pubring.kbx), the format which has superseded the old keyring format (pubring.gpg and secring.gpg), or the number of secret keys is a very simple task.

import gpg

c = gpg.Context()
seckeys = c.keylist(pattern=None, secret=True)
pubkeys = c.keylist(pattern=None, secret=False)

seclist = list(seckeys)
secnum = len(seclist)

publist = list(pubkeys)
pubnum = len(publist)

print("""
Number of secret keys:  {0}
Number of public keys:  {1}
""".format(secnum, pubnum))