Home > Blockchain >  problem when read files from local computer in pyscript
problem when read files from local computer in pyscript

Time:05-12

When I run the Python file, the file works correctly, but when I run the Python file with the script, it gives an error in read txt file(file not found).

enter image description here

how can i solve this problem؟

CodePudding user response:

See this issue report.

Code running in a webpage is sandboxed and can't freely access files on the computer hosting the browser.

(It would be a terrible security problem if just visiting a webpage would give the author of the page access to your files).

If you want to access a file on the user's computer, use <input type="file"> and have the user select it. I don't know if you can access it directly or if you would need to use a JavaScript FileReader and then pass the results from JS to PyScript … but one of those two approaches should be possible.

CodePudding user response:

Software written in any language (Python, JavaScript, WASM) cannot access local files directly. This is a browser security restriction. The browser can access files on behalf of your application using the <input type="file"> DOM element link.

In Python and JavaScript, you must provide the user with a file input selector. The user selects the file and your application can the retrieve the file data from the browser.

In JavaScript this functionality is implemented with the FileReader class. This class can be used in Python via the create_proxy() function to proxy event callbacks.

If you are just getting started with Pyscript, I have written a number of articles link.

  • Related