execute shell command from python script using subprocess module to search for a string in the directory of your choice

elementary script to execute shell commands using your very own python language…
i post this snippet here to demonstrate the power of python and yes an intro into the beautiful language called python and why i recommend learning it….

subprocess module in python helps to execute your favorite shell commands through python scripts…
for more references do look into python docs -> https://docs.python.org/2/library/subprocess.html

diving straight into the code…..
equivalent linux command for the below script is: ls -ltrh some_directory | grep search_term


#################################
#Script to search for a term in unix shell
#equivalent linux command: ls -ltrh some_directory | grep search_term
#author:nightslacker
#################################

#import sys module for taking system arguments from command line
import sys
#import subprocess module to execute unix shell commands from python script
import subprocess
#pass first argument as your directory
directory = sys.argv[1]
#pass second argument as the search string you want
search_term = sys.argv[2]
cmnd1 = subprocess.Popen(["ls", "-ltrh", directory], stdout=subprocess.PIPE)
cmnd2 = subprocess.Popen(["grep", search_term], stdin=cmnd1.stdout, stdout=subpr
ocess.PIPE)
output = cmnd2.communicate()[0]//cmnd2.communicate() returns a tuple containing (data of stdout,data of stderr)

print "*** Running ls -ltrh command on directory", directory, " and search term
:",search_term, "***\n", output

how to execute the above script from command line interface or CLI mode?
python anyname.py /home/nightslacker/Documents pythondocs

on executing the above line, the script checks in the Documents folder for any file with pythondocs in their filename and returns the output on your shell

keep slacking folks!!! cheers!!!