Open() is not defined in python scripts

HassOS 3.12
component/python_script
python version 3.8.2
python operation open() required.

I’m trying to use a small python script to edit a file within HASS.IO and Home Assistant.
It appears the open() is missing from the python sandbox, can anyone confirm and if so can it please be added. As an example, running a test python script with the below from the ‘python_script.test’ service produces an error message (after creating a file ‘test.py’ in python_scripts with the below content)
‘’’
f = open(‘RoofStatusFile3.txt’, ‘a’)
f.close()
‘’’

error message:
Error executing script: name ‘open’ is not defined
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/python_script/init.py”, line 196, in execute
exec(compiled.code, restricted_globals)
File “test.py”, line 1, in
NameError: name ‘open’ is not defined

All advice appreciated.

that’s because python_script uses RestrictedPython.
you can see roughly what’s included here (basically it’s this + some HA stuff).

you can always use a normal python script and call it as shell command

1 Like

Thanks very much, figured as much. Was looking for the easy solution!

My apologies, are you able to expand on this alternative solution?

I have a python script I wanted to initiate from Home Assistant, it creates a text file with some web data in. I can’t run it using python_script because of the problem you outlined.

But you’re saying I can with your solution? I’ve never used Shell Commands before so not entirely sure where to go from here.