Write text file in python script

I tried to write a state of an entity into a text file from a python script that I call from an automation, but I get an error regarding the open function, it says that it doesn’t recognize it, like if it was needed to load an additional module.

Someone knows how can I do that? Or someone knows an alternative method to write a text file from within Home Assistant?

Per the documentation:

The scripts are run in a sandboxed environment.

This means much of what you can do in Python is not available, including opening a file.

What are you trying to write to the file? You could possibly use the File Notifications integration.