Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: TextIOWrapper.seek() takes no keyword arguments

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

like image 954
ChidanshM Avatar asked Oct 20 '25 09:10

ChidanshM


1 Answers

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.

like image 109
J_H Avatar answered Oct 21 '25 21:10

J_H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!