Tracking Wordcount With Dropbox and Python

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 ="%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'))

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.

Last posts

  1. What I've Learned with a Raspberry Pi

    tags: RaspberryPiPython

  2. Markdown Tests


  3. First Post

    tags: pelicanpublishing

  4. Python with Evernote API Struggles – Part 3

    tags: APIEvernotePython

  5. Python with Evernote API Struggles - Part 2

    tags: APIEvernotePython

  6. Python with Evernote API Struggles

    tags: APIEvernotePython

  7. Daily Progress: Day 6

    tags: Daily

  8. Daily Progress: Day 5

    tags: Daily

  9. Daily Progress: Day 4

    tags: Daily

  10. Daily Progress: Day 3

    tags: Daily

  11. Daily Progress: Day 2

    tags: Daily

  12. Daily Progress: Day 1

    tags: Daily

  13. Copy Transactions to Spreadsheet - Part 2

    tags: FinanceSpreadsheeet

  14. Copy Transactions to Spreadsheet - Part 1

    tags: FinanceSpreadsheeet