简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM