* :ref:`json <json-commandline>`
* :ref:`mimetypes <mimetypes-cli>`
* :mod:`pdb`
-* :mod:`pickle`
+* :ref:`pickle <pickle-cli>`
* :ref:`pickletools <pickletools-cli>`
* :mod:`platform`
* :mod:`poplib`
.. pickletools.optimize() or the gzip module).
+.. _pickle-cli:
+
+Command-line interface
+----------------------
+
+The :mod:`pickle` module can be invoked as a script from the command line,
+it will display contents of the pickle files. However, when the pickle file
+that you want to examine comes from an untrusted source, ``-m pickletools``
+is a safer option because it does not execute pickle bytecode, see
+:ref:`pickletools CLI usage <pickletools-cli>`.
+
+.. code-block:: bash
+
+ python -m pickle pickle_file [pickle_file ...]
+
+The following option is accepted:
+
+.. program:: pickle
+
+.. option:: pickle_file
+
+ A pickle file to read, or ``-`` to indicate reading from standard input.
+
+
.. seealso::
Module :mod:`copyreg`
if __name__ == "__main__":
import argparse
+ import pprint
parser = argparse.ArgumentParser(
description='display contents of the pickle files')
parser.add_argument(
'pickle_file',
- nargs='*', help='the pickle file')
+ nargs='+', help='the pickle file')
args = parser.parse_args()
- if not args.pickle_file:
- parser.print_help()
- else:
- import pprint
- for fn in args.pickle_file:
- if fn == '-':
- obj = load(sys.stdin.buffer)
- else:
- with open(fn, 'rb') as f:
- obj = load(f)
- pprint.pprint(obj)
+ for fn in args.pickle_file:
+ if fn == '-':
+ obj = load(sys.stdin.buffer)
+ else:
+ with open(fn, 'rb') as f:
+ obj = load(f)
+ pprint.pprint(obj)