How to resolve TypeError: write() argument must be str, not int in Python

When programming errors TypeError: write() argument must be str, not int in Python, I will do the fix by passing the correct parameters to the write() function and checking the parameters before passing to the write() function. Post details below.

What causes the TypeError: write() argument must be str, not int error?

The error TypeError: write() argument must be str, not int occurs because the parameter you pass to the write() function is an integer instead of a string

Create a file ‘demo.txt‘.

Example: 

with open('demo.txt', 'a', encoding='utf-8') as fileObj:
    # Add data to the 'demo' file
    fileObj.write('Visit Learnshareit website')
    addToFile =  100
    
    # The write() function parameter is an integer, this will cause an error
    fileObj.write(addToFile)
with open('demo.txt', 'r', encoding='utf-8') as fileObj:
    data = fileObj.read()
    print(data)

Output:

Traceback (most recent call last):
  File "d:\vscode\Python\main.py", line 7, in <module>
    fileObj.write(addToFile)
TypeError: write() argument must be str, not int

How to solve this error?

The parameter passed to the write() function must be a string.

There is no doubt about this because the program has informed you that the write() function parameter you passed to the function is a false parameter. To fix the error, the parameter you pass to the write() function must be a string.

Example:

with open('demo.txt', 'a', encoding='utf-8') as fileObj:
    # Add data to the 'demo' file
    fileObj.write('Visit Learnshareit website')
    
    # Integers enclosed in single quotes will convert to a string
    addToFile = ' 100'
    fileObj.write(addToFile)
with open('demo.txt', 'r', encoding='utf-8') as fileObj:
    data = fileObj.read()
    print(data)

Output:

Visit Learnshareit website 100

Or you can use the str() function to convert an integer to a string.

Example:

with open('demo.txt', 'a', encoding='utf-8') as fileObj:
    # Add data to the 'demo' file
    fileObj.write('Visit Learnshareit website')
    
    # Use the str() function to convert to a string
    addToFile = str(100)
    fileObj.write(addToFile)
with open('demo.txt', 'r', encoding='utf-8') as fileObj:
    data = fileObj.read()
    print(data)

Output:

Visit Learnshareit website100

Check the value’s type before passing it to the write() function.

To avoid this error, you should check the variable’s data type before passing it to the write() function. There are many ways to check, such as using isinstance(), type(), or relational operators.

Example:

with open('demo.txt', 'a', encoding='utf-8') as fileObj:
    fileObj.write('Visit Learnshareit website')

addToFile = 100

# Use the isinstance() function to check the data type
with open('demo.txt', 'a', encoding='utf-8') as fileObj:
    if isinstance(addToFile, str) is True:
        fileObj.write(addToFile)
    else:
        convertToStr = str(addToFile)
        fileObj.write(convertToStr)

with open('demo.txt', 'r', encoding='utf-8') as fileObj:
    data = fileObj.read()
    print(data)

Output:

Visit Learnshareit website100

Example:

with open('demo.txt', 'a', encoding='utf-8') as fileObj:
    fileObj.write('Visit Learnshareit website')

addToFile = 100

# Use the type() function to check the data type
with open('demo.txt', 'a', encoding='utf-8') as fileObj:
    if type(addToFile) is str:
        fileObj.write(addToFile)
    else:
        convertToStr = str(addToFile)
        fileObj.write(convertToStr)

with open('demo.txt', 'r', encoding='utf-8') as fileObj:
    data = fileObj.read()
    print(data)

Output:

Visit Learnshareit website100

Summary

That is the cause and handling of the error TypeError: write() argument must be str, not int in Python, I want to convey to you. It’s important to remember that the write() function parameter is a string. If it’s not a string, convert it. Hope you get it fixed soon.

Leave a Reply

Your email address will not be published. Required fields are marked *