python - To redirect os.system() output to a .txt file -


i'm new python. have list of unix commmands ("uname -a","uptime","df -h","ifconfig -a","chkconfig --list","netstat -rn","cat /proc/meminfo","ls -l /dev") , want run them , redirect entire output .txt file. searched lot didn't proper solution, or understood things wrongly.

i'm able output on stdout loop can't redirect file.

def commandsoutput():     command = ("uname -a","uptime","df -h","ifconfig -a","chkconfig --list","netstat -rn","cat /proc/meminfo","ls -l /dev")      in command:         print (os.system(i))  commandsoutput() 

this answer uses os.popen, allows write output of command in file:

import os def commandsoutput():     commands = ("uname -a","uptime","df -h","ifconfig -a","chkconfig --list","netstat -rn","cat /proc/meminfo","ls -l /dev")     open('output.txt','a') outfile:         command in commands:             outfile.write(os.popen(command).read()+"\n") commandsoutput() 

Comments

Popular posts from this blog

PHP while loop dynamic rowspan -

timer - Java TimerTask cancel -

android - How to read a column value in parse.com without accessing an object or a pointer -