How to write PID to file in Python?

I get the current running script PID by os.getpid(), and try to store it in a file but I get an error that write() only accepts 'string' and not 'int'. So how to pass PID as a string to write()?

My code:

import os

outputFile = open('test.txt', "w")
pid = os.getpid()
outputFile.write(pid)
outputFile.close()

I have to use type convertor str() like this:

import os

with open('test.txt', 'w', encoding='utf-8') as f:
    f.write(str(os.getpid()))