Below are a few examples. These are largely to remind me of some key aspects that might be useful next time I have to write some scriptable task.
Line counting the functional way
#!/usr/bin/python import sys fp = open(sys.argv[1]) print reduce(lambda count,row: count+1, fp, 0)Executing Unix commands and piping the results
#!/usr/bin/python from subprocess import call, Popen, PIPE out = Popen("wc", stdin = PIPE, stdout = PIPE).communicate(Popen("ls", stdout = PIPE).communicate(None)[0]); print out[0]The start of a find command
from re import match from os import walk from os.path import join for root, dirs, files in walk('.'): print "\n".join([join(root, filename) for filename in files if match('.*\.py', filename)])
No comments:
Post a Comment