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
Post a Comment