def fetch_tweets(account_file, outfile, limit):
    """ Fetch up to limit tweets for each account in account_file and write to
    outfile. """
    print('fetching tweets for accounts in', account_file)
    outf = io.open(outfile, 'wt')
    for screen_name in iter_lines(account_file):
        print('\nFetching tweets for %s' % screen_name)
        for tweet in twutil.collect.tweets_for_user(screen_name, limit):
            tweet['user']['screen_name'] = screen_name
            outf.write('%s\n' % json.dumps(tweet, ensure_ascii=False))
            outf.flush()