Python/Shell

From Fundamental Ramen
< Python
Revision as of 07:33, 24 April 2020 by Tacoball (talk | contribs)
Jump to navigation Jump to search
TODO Code
Just run
subprocess.run(['dpkg', '-x', deb_path, ex_path])
Run with shell
(Necessary on Windows)
args = 'wmic product get name,version'.split(' ')
subprocess.run(args, shell=True)
Get stdout
args = 'wmic product get name,version'.split(' ')
completed = subprocess.run(args, shell=True, capture_output=True)
lines = completed.stdout.decode('cp950').split('\r\n')

Gracefully use subprocess.run() on Windows

import subprocess
import sys

def run(cmd):
    summary = ''
    message = ''
    indent  = '    '

    try:
        comp = subprocess.run(cmd, check=True, capture_output=True)
        stdout = comp.stdout.decode('cp950').strip()
        print('Okay:')
        for line in stdout.split('\n'):
            print(indent + line)
        print()
        return
    except subprocess.CalledProcessError as ex:
        summary = 'Returned error (%s):' % type(ex).__name__
        message = str(ex)
    except FileNotFoundError as ex:
        summary = 'No such executable (%s):' % type(ex).__name__
        message = str(ex)
    except Exception as ex:
        summary = 'Unknown error (%s):' % type(ex).__name__
        message = str(ex)

    print(summary)
    print(indent + message)
    print()

def main():
    run(['tasklist.exe', '/fi', 'imagename eq line.exe'])
    run(['tasklist.exe', '/fuck', 'imagename eq line.exe'])
    run(['tasklist.ex', '/fi', 'imagename eq line.exe'])

if __name__ == '__main__':
    main()