Skip to content

Instantly share code, notes, and snippets.

@cdunklau
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save cdunklau/20cab47619d052745a58 to your computer and use it in GitHub Desktop.

Select an option

Save cdunklau/20cab47619d052745a58 to your computer and use it in GitHub Desktop.
Progress-reporting line reader
import os
class ProgressLineReader(object):
def __init__(self, filepath, mode='r'):
self.path = filepath
self.total_size = os.stat(filepath).st_size
self.already_read = 0
self.fobj = open(filepath, mode)
def __iter__(self):
for line in self.fobj:
self.already_read += len(line)
yield line
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
@property
def progress(self):
"""Percentage read"""
return 100.0 * self.total_size / self.already_read
def close(self):
self.fobj.close()
import csv
from progress import ProgressLineReader
with ProgressLineReader('path/to/file.csv') as progressreader:
for row in csv.reader(progressreader):
print 'Got row: {0}'.format(row)
print 'Read {0.already_read} of {0.total_size}, {0.progress} %'.format(progressreader)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment