I've been wanting to get more involved in self tracking, but never had enough programming knowledge to make it happen. Recently, I've put all of my writing in Dropbox so that it would be easier to access from multiple devices. Combining that with some basic knowledge in Python means that I can begin tracking the number of words I'm producing, no matter what file it is in.
The requirements for my files is that I can access from any device and write/preview in Markdown. I'm using Editorially on iOS, which can save to a specific Dropbox folder. On Windows I use MarkdownPad2, and work in the same folder. Any application that can modify/save to a single Dropbox directory would work, though.
The Python part is pretty simple and involves looping through all files in a directory and getting some basic statistics and appending those to a csv file.
# track_words_in_directory # # Run at startup, and add to csv file, the statistics for a given directory # # import os import csv import datetime BASE_DIR = r'C:\......\Dropbox\Apps\Editorial' OUTPUT_FILE = r'C:\.......\Dropbox\Programming\word_counts.csv' file_count = 0 file_lines = 0 file_words = 0 file_chars = 0 for dirname, dirnames, filenames in os.walk(BASE_DIR): for filename in filenames: my_file = os.path.join(dirname, filename) file_count += 1 with open(my_file, 'r') as f: for line in f: file_lines += 1 words = line.split() file_words += len(words) file_chars += len(line) print 'files: %s' % file_count print 'lines: %s' % file_lines print 'words: %s' % file_words print 'chars: %s' % file_chars current_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") output_line = (current_date, file_count, file_lines, file_words, file_chars) print output_line out_file = open('tracking.csv', 'ab') #wb first, a for append writer = csv.writer(out_file) #writer.writerow(('date', 'file_count', 'file_lines', 'file_words', 'file_char')) writer.writerow(output_line) out_file.close()
The one thing I don't know how to do yet, is setup the script to run every so often.
Going further, it would be interesting to add some notifications more frequently while I was writing or to look for other measurable things to compare this with.