pyscript.fs
This module provides an API for mounting directories from the user's local filesystem into the browser's virtual filesystem. This means Python code, running in the browser, can read and write files on the user's local machine.
Warning
This API only works in Chromium-based browsers (Chrome, Edge, Vivaldi, Brave, etc.) that support the File System Access API.
The module maintains a mounted dictionary that tracks all currently mounted
paths and their associated filesystem handles.
from pyscript import fs, document, when
# Mount a local directory to the `/local` mount point in the browser's
# virtual filesystem (may prompt user for permission).
await fs.mount("/local")
# Alternatively, mount on a button click event. This is important because
# if the call to `fs.mount` happens after a click or other transient event,
# the confirmation dialog will not be shown.
@when("click", "#mount-button")
async def handler(event):
await fs.mount("/another_dir")
# Work with files in the mounted directory as usual.
with open("/local/example.txt", "w") as f:
f.write("Hello from PyScript!")
# Ensure changes are written to local filesystem.
await fs.sync("/local")
# Clean up when done.
await fs.unmount("/local")
mounted = {}
module-attribute
Global dictionary tracking mounted paths and their filesystem handles.
mount(path, mode='readwrite', root='', id='pyscript')
async
Mount a directory from the local filesystem to the virtual filesystem
at the specified path mount point. The mode can be "readwrite" or
"read" to specify access level. The root parameter provides a hint
for the file picker starting location. The id parameter allows multiple
distinct mounts at the same path.
On first use, the browser will prompt the user to select a directory and grant permission.
from pyscript import fs
# Basic mount with default settings.
await fs.mount("/local")
# Mount with read-only access.
await fs.mount("/readonly", mode="read")
# Mount with a hint to start in Downloads folder.
await fs.mount("/downloads", root="downloads")
# Mount with a custom ID to track different directories.
await fs.mount("/project", id="my-project")
If called during a user interaction (like a button click), the permission dialog may be skipped if permission was previously granted.
Source code in pyscript/fs.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
sync(path)
async
Synchronise the virtual and local filesystems for a mounted path.
This ensures all changes made in the browser's virtual filesystem are written to the user's local filesystem, and vice versa.
from pyscript import fs
await fs.mount("/local")
# Make changes to files.
with open("/local/data.txt", "w") as f:
f.write("Important data")
# Ensure changes are written to local disk.
await fs.sync("/local")
This is automatically called by unmount(), but you may want to call it explicitly to ensure data persistence at specific points.
Source code in pyscript/fs.py
unmount(path)
async
Unmount a directory, specified by path, from the virtual filesystem.
This synchronises any pending changes and then removes the mount point,
freeing up memory. The path can be reused for mounting a different
directory.
from pyscript import fs
await fs.mount("/local")
# ... work with files ...
await fs.unmount("/local")
# Path can now be reused.
await fs.mount("/local", id="different-folder")
This automatically calls sync() before unmounting to ensure no data
is lost.
Source code in pyscript/fs.py
revoke(path, id='pyscript')
async
Revoke filesystem access permission and unmount for a given
path and id combination.
This removes the stored permission for accessing the user's local
filesystem at the specified path and ID. Unlike unmount(), which only
removes the mount point, revoke() also clears the permission so the
user will be prompted again on next mount.
from pyscript import fs
await fs.mount("/local", id="my-app")
# ... work with files ...
# Revoke permission (user will be prompted again next time).
revoked = await fs.revoke("/local", id="my-app")
if revoked:
print("Permission revoked successfully")
After revoking, the user will need to grant permission again and
select a directory when mount() is called next time.