Python/Shell: Difference between revisions
< Python
Jump to navigation
Jump to search
No edit summary |
|||
| (3 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
|- | |- | ||
| Just run || | | Just run || | ||
<source lang="python3> | <source lang="python3"> | ||
subprocess.run(['dpkg', '-x', deb_path, ex_path]) | subprocess.run(['dpkg', '-x', deb_path, ex_path]) | ||
</source> | </source> | ||
|- | |- | ||
| Run with shell<br/>(Necessary on Windows) || | | Run with shell<br/>(Necessary on Windows) || | ||
<source lang="python3> | <source lang="python3"> | ||
args = 'wmic product get name,version'.split(' ') | args = 'wmic product get name,version'.split(' ') | ||
subprocess.run(args, shell=True) | subprocess.run(args, shell=True) | ||
| Line 14: | Line 14: | ||
|- | |- | ||
| Get stdout || | | Get stdout || | ||
<source lang="python3"> | |||
args = 'wmic product get name,version'.split(' ') | |||
completed = subprocess.run(args, shell=True, capture_output=True) | |||
lines = completed.stdout.decode('cp950').split('\r\n') | |||
</source> | |||
|} | |||
== Gracefully use subprocess.run() on Windows == | |||
<source lang="python3"> | |||
import subprocess | |||
import sys | |||
def run(cmd): | |||
summary = '' | |||
message = '' | |||
indent = ' ' | |||
charset = 'cp950' | |||
try: | |||
comp = subprocess.run(cmd, check=True, capture_output=True) | |||
summary = "Okay:" | |||
message = comp.stdout.decode(charset).strip() | |||
except subprocess.CalledProcessError as ex: | |||
summary = 'Error returned (%s):' % type(ex).__name__ | |||
message = "return code: %d\n%s" % (ex.returncode, ex.stderr.decode(charset)) | |||
message = message.strip() | |||
except FileNotFoundError as ex: | |||
summary = 'No such executable (%s):' % type(ex).__name__ | |||
message = ex.strerror | |||
except Exception as ex: | |||
summary = 'Unknown error (%s):' % type(ex).__name__ | |||
message = str(ex) | |||
print(summary) | |||
for line in message.split('\n'): | |||
print(indent + line) | |||
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() | |||
</source> | |||
Latest revision as of 08:53, 24 April 2020
| 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 = ' '
charset = 'cp950'
try:
comp = subprocess.run(cmd, check=True, capture_output=True)
summary = "Okay:"
message = comp.stdout.decode(charset).strip()
except subprocess.CalledProcessError as ex:
summary = 'Error returned (%s):' % type(ex).__name__
message = "return code: %d\n%s" % (ex.returncode, ex.stderr.decode(charset))
message = message.strip()
except FileNotFoundError as ex:
summary = 'No such executable (%s):' % type(ex).__name__
message = ex.strerror
except Exception as ex:
summary = 'Unknown error (%s):' % type(ex).__name__
message = str(ex)
print(summary)
for line in message.split('\n'):
print(indent + line)
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()