Home > Software engineering >  TypeError: TextIOWrapper.seek() takes no keyword arguments
TypeError: TextIOWrapper.seek() takes no keyword arguments

Time:04-22

I wanted to seek to the start of the file of to write from the start. In the documentation of python 3.9 io.IOBase.seek it is displayed seek has a parameter "whence" yet an error is being displayed:

TypeError: TextIOWrapper.seek() takes no keyword arguments

my code is:

with open("t.txt",'a ') as f:
    f.seek(0,)
    print(f.readlines())
    f.seek(0,whence=0)
    f.write("12\n23\n32")

I have use "a " as I want to preserve the contains of the file when it is opened as well edit later.

I wanted to edit the contains from the start that's why I used whence = 0, as it would help me edit from start of the stream

CodePudding user response:

Yeah, it's a little bit weird.

Take a look at help(f.seek):

Help on built-in function seek:

seek(cookie, whence=0, /) method of _io.TextIOWrapper instance

Note the / slash. https://stackoverflow.com/a/24735582/8431111

It says "no keywords, please!".

You can specify f.seek(0), or f.seek(0, 0). You just can't name that 2nd parameter whence. It is helpful documentation in the signature, but you can't name it in the call.

  • Related