возможно немного проще в использовании ..
def LengthOfFile(f):
""" Get the length of the file for a regular file (not a device file)"""
currentPos=f.tell()
f.seek(0, 2) # move to end of file
length = f.tell() # get current position
f.seek(currentPos, 0) # go back to where we started
return length
def BytesRemaining(f,f_len):
""" Get number of bytes left to read, where f_len is the length of the file (probably from f_len=LengthOfFile(f) )"""
currentPos=f.tell()
return f_len-currentPos
def BytesRemainingAndSize(f):
""" Get number of bytes left to read for a regular file (not a device file), returns a tuple of the bytes remaining and the total length of the file
If your code is going to be doing this alot then use LengthOfFile and BytesRemaining instead of this function
"""
currentPos=f.tell()
l=LengthOfFile(f)
return l-currentPos,l
if __name__ == "__main__":
f=open("aFile.data",'r')
f_len=LengthOfFile(f)
print "f_len=",f_len
print "BytesRemaining=",BytesRemaining(f,f_len),"=",BytesRemainingAndSize(f)
f.read(1000)
print "BytesRemaining=",BytesRemaining(f,f_len),"=",BytesRemainingAndSize(f)