Wednesday 12 September 2012

How to run shell commands in python

How to run shell commands in python

Inorder to run your shell commands in python script, you may need to import an module named os. Below is the simple example on how it actually works

import os
os.system('ls -l /boot/')
total 22784
-rw-r--r-- 1 root root 791023 Apr 11 05:56 abi-3.2.0-23-generic
-rw-r--r-- 1 root root 140279 Apr 11 05:56 config-3.2.0-23-generic
drwxr-xr-x 3 root root 12288 Aug 14 18:51 grub
-rw-r--r-- 1 root root 14163334 Aug 14 18:51 initrd.img-3.2.0-23-generic
-rw-r--r-- 1 root root 176764 Nov 27 2011 memtest86+.bin
-rw-r--r-- 1 root root 178944 Nov 27 2011 memtest86+_multiboot.bin
-rw------- 1 root root 2884358 Apr 11 05:56 System.map-3.2.0-23-generic
-rw-r--r-- 1 root root 4965840 Apr 25 21:41 vmlinuz-3.2.0-23-generic