Search code examples
python-3.7

write() takes no keyword arquments in Python


I get this error:

TypeError: write() takes no argument

I tried to write as str(k), but nothing helps

f = open("lab8_674046492_answers.txt","w")
for k,v in sorted(course_catalog.items()):
    f.write(k,end='')
    f.write(':',v)
f.close()
f = open("lab8_674046492_answers.txt","r")
print(f.read())

I expected the keys and the values of my list. How can I fix this?


Solution

  • f = open("lab8_674046492_answers.txt","w")
    for k,v in sorted(course_catalog.items()):
        f.write(k,'')//why use "end="
        f.write(':',v)
    f.close()
    f = open("lab8_674046492_answers.txt","r")
    print(f.read())
    

    There is no need to use "end=''" in a function, just f.write(k,'') Or, if you are trying to actually write end='', make sure to put it in quotes:

    f = open("lab8_674046492_answers.txt","w")
    for k,v in sorted(course_catalog.items()):
        f.write(k, "end=''")//Make sure to put it in quotes
        f.write(':',v)
    f.close()
    f = open("lab8_674046492_answers.txt","r")
    print(f.read())