def CopyReversedLines(instream, outstream, blocksize=(2 ** 16)):
 line_count = 0
 instream.seek(0, 2)
 last_block = (instream.tell() // blocksize)
 spillover = ''
 for iblock in xrange((last_block + 1), (-1), (-1)):
  instream.seek((iblock * blocksize))
  data = instream.read(blocksize)
  lines = data.splitlines(True)
  lines[(-1):] = ''.join((lines[(-1):] + [spillover])).splitlines(True)
  if (lines and (not lines[(-1)].endswith('\n'))):
   lines[(-1)] += '\n'
  lines.reverse()
  if (lines and (iblock > 0)):
   spillover = lines.pop()
  if lines:
   line_count += len(lines)
   data = ''.join(lines).replace('\x00', '\n DCTB ')
   outstream.write(data)
 return line_count

def CopyReversedLines ( instream , outstream , blocksize = ( 2 ** 16 ) ) : line_count = 0 instream . seek ( 0 , 2 ) last_block = ( instream . tell ( ) // blocksize ) spillover = '' for iblock in xrange ( ( last_block + 1 ) , ( - 1 ) , ( - 1 ) ) : instream . seek ( ( iblock * blocksize ) ) data = instream . read ( blocksize ) lines = data . splitlines ( True ) lines [ ( - 1 ) : ] = '' . join ( ( lines [ ( - 1 ) : ] + [ spillover ] ) ) . splitlines ( True ) if ( lines and ( not lines [ ( - 1 ) ] . endswith ( '\n' ) ) ) : lines [ ( - 1 ) ] += '\n' lines . reverse ( ) if ( lines and ( iblock > 0 ) ) : spillover = lines . pop ( ) if lines : line_count += len ( lines ) data = '' . join ( lines ) . replace ( '\x00' , '\n DCTB ' ) outstream . write ( data ) return line_count
copy lines from input stream to output stream in reverse order .