How to obtain man page contents in Python?
-
05-07-2021 - |
Question
im running linux and i want to import some man pages to my application.
i came up with this:
p = subprocess.Popen(('man %s' % manTopic,), shell = True, stdout = subprocess.PIPE)
stdout, stderr = p.communicate()
if stdout:
but its no good, man is displaying only first page and blocks my applicationon
How can i obtain man page with Python?
La solution
Try:
p = subprocess.Popen(('man -P cat %s' % manTopic,), shell = True)
stdout, stderr = p.communicate()
if stdout:
instead -- the "-P" option overrides the pager program used by the "man" command.
Autres conseils
You can grab the whole output of a command with check_output
. Furthermore, using a shell is not necessary and might even make your application vulnerable to a shell injection attack and is strongly discouraged.
import subprocess
pagename = 'man'
manpage = subprocess.check_output(['man', pagename])
Note that using man
will give you output formatted for a terminal. If you want to have it formatted differently, you'll have to
- call
man -w <name>
to get the location of the manpage, - probably decompress the manual page,
- feed it to
groff
using the-T
option to select the type of output you want.
When calling groff
, don't forget to load the correct macro's.
On FreeBSD I tend use groff -Tlatin1 -mandoc <file>
to get text output.