Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Works on my machine (Mac OS X 10.6.8, Python 2.7.1)
- # Use with caution. Count early and often.
- import os
- import sys
- import time
- from subprocess import Popen, PIPE
- def word_count_loop(file):
- old_stat = 0
- while True:
- new_stat = os.stat(file).st_mtime
- if new_stat != old_stat:
- command = 'wc -w < %s' % file
- p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
- # Retreive word count
- words, _ = p.communicate()
- # Stip whitespace
- words = words.strip()
- # Clear screen
- os.system('clear')
- # Write result to screen
- sys.stdout.write("Words: %s" % words)
- sys.stdout.flush()
- old_stat = new_stat
- continue
- time.sleep(.25)
- if __name__ == '__main__':
- # Check command line arg length
- if len(sys.argv) > 1:
- # Check if file exists
- if os.path.exists(sys.argv[1]):
- word_count_loop(sys.argv[1])
- else:
- print "Error: File Not Found"
- else:
- # No command line args given. Show usage info...
- print " Usage:"
- print " python wcloop.py <filepath>"
- print " Example:"
- print " python wcloop.py ~/Documents/file-that-I-want-to-repeatedly-check-with-wc.txt"
Add Comment
Please, Sign In to add comment