Last Updated: February 25, 2016
·
503
· chauvd

Shell Execute on Multiple Platforms

Need to execute shell commands on multiple platforms? Trying to execute a command to activate pdf viewer on a system regardless of the OS is incredibly simple.

 pdf_file_path = doc_path + '/' + 
                 outfile + '.pdf'

sys_plfrm = platform.system()
cmd = {'Darwin' :'open ', 
       'Windows':'start ',
       'Linux'  :'xpdf '}

try:
    call([cmd[sys_pltfrm] + pdf_file_path], shell=True)
except OSError as err:
    sys.exit('Error opening pdf:\n\t{}'.format(err))

I realize that having shell=True is not the safest with 'unsanitized' input

Nothing fancy, but a neat example for multi-system use...