Tag Archives: Python_

Python_:TypeError: write() argument must be str, not int

Detailed map traversal, teach you to master the complex gremlin query debugging method>>>

Python_ Error: typeerror: write() argument must be STR, not int

When running the file write operation, an error is reported: typeerror: write() argument must be STR, not int

Upper Code:

fp = open("1023a.txt","w+")
l = [1023,1024,1025]
fp.writelines(l)
fp.seek(0,0)
fp.read()
fp.close()

The operation effect is as follows:

Cause analysis:
writelines() can pass in both a string and a character sequence, and write the character sequence to a file
note: writelines must pass in a character sequence, not a number sequence

It can be changed as follows:

fp = open("1023b.txt","w+")
l = ["1023","1024","1025"]
fp.writelines(l)
fp.seek(0,0)
fp.read()
fp.close()

The following results are:

Python Exception: TypeError: write() argument must be str, not list

When writing to a file, an error is reported: TypeError: write() argument must be str, not list

Reason: The content written by python should be of string type

Code.

fp = open(“a.txt”,”w”)
fp.write([1,2,3])
fp.close()

>>> fp = open("a.txt","w")
>>> fp.write([1,2,3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not list
>>> fp.close()

If the writing content is string type, OK

fp = open("a.txt","w")
fp.write('[1,2,3]')#Processing file contents to string type
fp.close()