Answers for "python shell command get output"

1

execute command and get output python

# You can use following commands to run any shell command. I have used them on ubuntu.

import os
os.popen('your command here').read()

# Note: This is deprecated since python 2.6. Now you must use subprocess.Popen. Below is the example

import subprocess

p = subprocess.Popen("Your command", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
print p.split("n")
Posted by: Guest on October-29-2021
2

python run command and read output

#!/usr/bin/python
import subprocess, sys
## command to run - tcp only ##
cmd = "/usr/sbin/netstat -p tcp -f inet"
## run it ##
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
## But do not wait till netstat finish, start displaying output immediately ##
while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()
Posted by: Guest on August-10-2020
2

python run command and read output

>>> subprocess.check_output(['ls', '-l'])
b'total 0n-rw-r--r--  1 memyself  staff  0 Mar 14 11:04 filesn'
Posted by: Guest on August-10-2020

Code answers related to "python shell command get output"

Python Answers by Framework

Browse Popular Code Answers by Language