Fix Python – Write to UTF-8 file in Python

I’m really confused with the codecs.open function. When I do:
file = codecs.open(“temp”, “w”, “utf-8”)
file.write(codecs.BOM_UTF8)
file.close()

It gives me the error

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position
0: ordinal not in range(128)

If I do:
file = open(“temp”, “w”)
file.write(codecs.BOM_UTF8)
file.close()

It w….