import subprocess
cmd = 'ifconfig -a'
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
while True:
out = p.stderr.read(1)
if out == '' and p.poll() != None:
break
if out != '':
sys.stdout.write(out)
sys.stdout.flush()
<<<HOW TO CMD SENT AND IT'S OUTPUT>>>
file = open('outputFile.txt', 'w+')
file.write(out)
file.close()