#!/usr/bin/env python2.7 import platform import struct if platform.system() == "Java": print "Jython %s" % platform.python_version() else: print "Python %s" % platform.python_version() print platform.version() # First write the file f = file("seek-test.out", "wb") for x in range(256): f.write(chr(x)) f.close() # Reopen it f = file("seek-test.out", "rb+") # Read forward from beginning for x in range(256): assert f.tell() == x, "before read: pos should be %d but was %d" % (x, f.tell()) c = struct.unpack("B", f.read(1))[0] assert f.tell() == x+1, "after read: pos should be %d but was %d" % (x+1, f.tell()) assert c == x, "expected %d; got %d" % (x, c) # Read backward from end f.seek(-1, 2) # seek to end for x in range(255, -1, -1): assert f.tell() == x, "before read: pos should be %d but was %d" % (x, f.tell()) c = ord(f.read(1)) assert c == x, "expected %d; got %d" % (x, c) assert f.tell() == x+1, "after read: pos should be %d but was %d" % (x+1, f.tell()) if x > 0: f.seek(-2, 1) print "No errors"