1

I have read the "seek" function documentation and I know there are as arguments an offset and a whence argument. I can use this function perfectly when only using the offset argument, but if I specify the whence argument, "seek" only works if the offset is 0. If not, this is what happens:

 f = open ('New_file.txt', 'w')
 f.write ('This is a text')
 f.seek (-5, 2)

Error:

--------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-1-d369dbea9034> in <module>()
      1 f = open ('New_file.txt', 'w')
      2 f.write ('This is a text')
----> 3 f.seek (-5, 2)

UnsupportedOperation: can't do nonzero end-relative seeks`

I have used help (f.seek) and it says I can use negative numbers in the offset, so can somebody tell me why I have this problem?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
davejason
  • 11
  • 1
  • 2
  • 3
    From the [documentation](http://docs.python.org/3.2/tutorial/inputoutput.html#methods-of-file-objects) for Python 3.2 and up: > In text files (those opened without a `b` in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with `seek(0, 2)`). – ymmx Jan 02 '18 at 15:53
  • Thank you. I see the problem now. – davejason Jan 03 '18 at 17:25

0 Answers0