Home  >  Q&A  >  body text

Pyscript says there is no directory but there is

I'm running into a frustrating problem, pyscript says the directory doesn't exist. But it is. I have a file called "pot1" and pyscript doesn't seem to see it. I have rewritten the directory over 893 times and trying to find the problem, I have the directory written to the file correctly.

<py-script>
    pot1 = open("C:/Plant Lab/sav/garden/pots/pot1", "r").readline()
</py-script>

But I only get this.

Traceback (most recent call last):
  File "/lib/python3.10/_pyodide/_base.py", line 460, in eval_code
    .run(globals, locals)
  File "/lib/python3.10/_pyodide/_base.py", line 306, in run
    coroutine = eval(self.code, globals, locals)
  File "<exec>", line 1, in <module>
FileNotFoundError: [Errno 44] No such file or directory: 'C:/Plant Lab/sav/garden/pots/pot1'

I tried searching on Google but ended up getting very basic answers like "Did you write the table of contents correctly?" and I didn't know what else to do. I've tried using \ instead of /. Tried to find out what directory it is but CORS won't let me. In the meantime, I'll try again and again.

P粉170858678P粉170858678282 days ago410

reply all(1)I'll reply

  • P粉366946380

    P粉3669463802024-01-11 10:13:21

    When using pyscript, the code runs in a virtual file system. If you run

    <html>
      <head>
        <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
        <script defer src="https://pyscript.net/latest/pyscript.js"></script>
      </head>
      <body>
        <py-script> import os;print(os.getcwd())</py-script>
      </body>
    </html>

    You should get /home/pyodide, which is the root directory of this virtual file system. Your folders and files are not directly accessible through this virtual system (this is a limitation of Javascript applications).

    The easiest way to access data from the file system is to use the tag to get the file (more info here).

    <html>
      <head>
        <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
        <script defer src="https://pyscript.net/latest/pyscript.js"></script>
      </head>
      <body>
        <py-config> 
          [[fetch]] 
          files = ["/path/to/your/file"] </py-config>
        <py-script>
          pot1 = open("/path/to/your/file", "r").readline()
          print(pot1)
        </py-script>
      </body>
    </html>

    If you update the file like this and try to access it, you will still get the error. This is because the fetch method is usually used to load content from a remote server, but in your case you can run a simple python web server by running

    python3 -m http.server

    Then visit your page http://0.0.0.0:8000/yourfile.html.

    reply
    0
  • Cancelreply