Home > Blockchain >  TypeError: cannot concatenate 'str' and 'int' objects f.write python
TypeError: cannot concatenate 'str' and 'int' objects f.write python

Time:01-16

I want to make the script save the result to the save.txt file I am running with python2.7 and they are writing an error

Traceback (most recent call last): File "point9.py", line 7, in <module> f.write("" data "" "\n") TypeError: cannot concatenate 'str' and 'int' objects

Code:

data = 3673256654

print(data)


f = open("save.txt", 'a') 
f.write(""   data   ""   "\n") 
f.close()

CodePudding user response:

This expression doesn't do what you want:

""   data   ""   "\n"

Prefer

f"{data}\n"

Some years ago they sunsetted the interpreter you're using. I don't know offhand if f-strings were ever back ported to python2.

You could use str(data) "\n" for same effect. Or any of the several .format() variants. (Not sure why you appended empty string a couple of times, as that does literally nothing.)

  • Related