Hi,
I am trying to store some results to a txt file, but I got the following error:
f = open(filename, 'w') ^ NameError: name 'open' is not defined
Can anyone please help?
Thank you very much.
Hi,
I am trying to store some results to a txt file, but I got the following error:
f = open(filename, 'w') ^ NameError: name 'open' is not defined
Can anyone please help?
Thank you very much.
From the docs (https://hacs-pyscript.readthedocs.io/en/latest/reference.html#avoiding-event-loop-i-o):
Currently the built-in functions that do I/O, such as
open
,read
andwrite
are not supported to avoid I/O in the main event loop, and also to avoid security issues if people share pyscripts. Also, the
- put the code in a separate native Python module, so that functions like
open
,read
andwrite
are available, and call the function in that module from pyscript usingtask.executor
. See Importing for how to set Python’ssys.path
to import a local Python module.- you could use the
os
package (which can be imported by settingallow_all_imports
) and calling the low-level functions likeos.open
andos.read
usingtask.executor
to wrap every function.
You seem to have the solution to this same problem I have! It is driving me up the wall; I can’t find an example anywhere. I have this:
file = task.executor(os.open, filepath, “w”)
But it generates an error stating it can’t interpret a string as an integer. This must refer to the write (“w”) argument.
My question is: how do I pass this argument to the task executor?
Thanks in advance…
any news on this ? I have the same problem
I’ve had another desperate battle with this, using chatGPT, but my frustration levels are through the roof right now. Why did they make it impossible to write and read a simple text ot JSON file!?! Really annoying…
Sorry, but I give up, at least until my cortisol is at a normal level again.
I was having the same problem.
I fixed it by switching from os.open to io.open