Downloads

From Thalesians
Jump to: navigation, search

Libraries

Utilities

backwards_text_file.py: Reading a text file backwards

[ Download backwards_text_file.py. ]

Reading a text file backwards is a relatively common task. Let me explain first what I mean by backwards: you read the file line by line, starting from the last line and progressing towards the first.

Why would you need this? Imagine that you have a large CSV (comma separated value) with numerous records sorted in ascending order by date/time. You want to read the last N records. Using the standard text file input/output machinery you would probably end up reading the entire file, discarding all but the last N records. Extremely wasteful. Chances are you will have more than one such file.

I have written a Python module to help you: backwards_text_file.py. It is really a port of Uri Guttman's Perl module File::ReadBackwards, which is also attempting to behave like Greg Ward's standard Python module distutils.text_file, and should be adaptable.

This module is very easy to use:

  1. from backwards_text_file import BackwardsTextFile
  2.  
  3. in_file = BackwardsTextFile("test.csv")
  4.  
  5. result = in_file.readlines()
  6.  
  7. print result
  8.  
  9. in_file.close()

This will read the entire file backwards. To read it line by line, as you will need to do in most circumstances, use the following:

  1. from backwards_text_file import BackwardsTextFile
  2.  
  3. in_file = BackwardsTextFile("test.csv")
  4.  
  5. line = in_file.readline()
  6. while not line is None:
  7. print line
  8. line = in_file.readline()
  9.  
  10. in_file.close()

That's it. I'm quite keen on testing this module on various operating systems and in various environment, so I would appreciate your feedback. Without further ado,

[ Download backwards_text_file.py. ]

Personal tools