Gareth Allen wrote: > Hi all, > > I'm trying to get the output of a command and split it into a list that I can > process. What is the > best way to go about doing this? In bash I would use tools like grep, sed awk > etc. > > Here's an example: > > ifconfig > > lo Link encap:Local Loopback > inet addr:127.0.0.1 Mask:255.0.0.0 > inet6 addr: ::1/128 Scope:Host > UP LOOPBACK RUNNING MTU:16436 Metric:1 > RX packets:84253 errors:0 dropped:0 overruns:0 frame:0 > TX packets:84253 errors:0 dropped:0 overruns:0 carrier:0 > collisions:0 txqueuelen:0 > RX bytes:11763964 (11.2 MiB) TX bytes:11763964 (11.2 MiB) > > I would like to end up with something like this in a file: > > <unix timestamp>,lo,rx_errors=0,rx_dropped=0,rx_overruns=0,rx_frame=0 >
Look at the subprocess module it is better than os.system(). http://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate >>> process = subprocess.Popen(['/sbin/ifconfig'], stdout=subprocess.PIPE) >>> output = process.communicate() # block until job finishes then return # (stdout, stderr) >>> print output[0] # do something with output It is sometimes tempting using shell=True (it is False by default), but it can be insecure and should be avoided when possible. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor