1

Shouldnt it create a file and write asd in it? When it prints out fgh.

   <!DOCTYPE html>
<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>
        f = open("file.txt", "a", encoding="UTF-8")
        f.write("asd")
        print("fgh")
    </py-script>
  </body>
</html>
  • as @kenan238 mentioned, it would be a major security risk to be able to write files on the client's computer. what you probably want to do is send the file to the server? there are numerous way to do that in JS/Python depending of your needs and the of language you run on the server side – Je Je Dec 16 '22 at 19:30
  • you can look at cookies as well, which would an alternative. – Je Je Dec 16 '22 at 19:33
  • You say `fgh` printed? Add `import os;print(os.getcwd())` to see if the file is there. – tdelaney Dec 16 '22 at 19:36
  • 1
    Your code is writing to the browser's virtual file system which is created new on a page load. See my article series on PyScript: https://www.jhanley.com/blog/category/pyscript/ – John Hanley Dec 16 '22 at 23:25

0 Answers0