I am trying to get output of a shell command in a txt file. so far till now i have succeeded getting output of my script this way
import subprocess
import logging
logging.basicConfig(level=logging.DEBUG,filename='E:\FYP\FYPPP\AMAPT\hola.txt',filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
command="sh amapt.sh"
logging.info(subprocess.Popen(command,stdout=subprocess.PIPE,shell=True).stdout.read())
This do displays the output of my script (amapt.sh) as well as stores the output in a txt file too. But i have to get the output of "sh amapt.sh -s try.apk" and store it in txt file as above. i tried doing it by this way
command="sh amapt.sh -s try.apk"
logging.info(subprocess.Popen(command,stdout=subprocess.PIPE,shell=True).stdout.read())
But i am getting this error on running this code
tput: terminal attributes: No such device or address
tput: terminal attributes: No such device or address
Why not use subprocess.call? You can use the stdout
argument to direct to a filestream.
from subprocess import call
command = ['echo','Hello!']
call(command, stdout=open('output.txt','w'))