Guest User

Untitled

a guest
Jan 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. # Works on my machine (Mac OS X 10.6.8, Python 2.7.1)
  2. # Use with caution. Count early and often.
  3.  
  4. import os
  5. import sys
  6. import time
  7. from subprocess import Popen, PIPE
  8.  
  9. def word_count_loop(file):
  10. old_stat = 0
  11. while True:
  12. new_stat = os.stat(file).st_mtime
  13. if new_stat != old_stat:
  14. command = 'wc -w < %s' % file
  15. p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
  16.  
  17. # Retreive word count
  18. words, _ = p.communicate()
  19.  
  20. # Stip whitespace
  21. words = words.strip()
  22.  
  23. # Clear screen
  24. os.system('clear')
  25.  
  26. # Write result to screen
  27. sys.stdout.write("Words: %s" % words)
  28. sys.stdout.flush()
  29.  
  30. old_stat = new_stat
  31. continue
  32. time.sleep(.25)
  33.  
  34. if __name__ == '__main__':
  35. # Check command line arg length
  36. if len(sys.argv) > 1:
  37. # Check if file exists
  38. if os.path.exists(sys.argv[1]):
  39. word_count_loop(sys.argv[1])
  40. else:
  41. print "Error: File Not Found"
  42. else:
  43. # No command line args given. Show usage info...
  44. print " Usage:"
  45. print " python wcloop.py <filepath>"
  46. print " Example:"
  47. print " python wcloop.py ~/Documents/file-that-I-want-to-repeatedly-check-with-wc.txt"
Add Comment
Please, Sign In to add comment