Files
- class files.Files[source]
Interface for the PythonAnywhere Files API.
This class uses the get_api_endpoint function from
pythonanywhere_core.baseto construct the API URL, which is stored in the class variablebase_url. It then calls thecall_apimethod with the appropriate arguments to perform file-related actions.- Supported Endpoints:
GET, POST, and DELETE for the files path endpoint.
POST, GET, and DELETE for the file sharing endpoint.
GET for the tree endpoint.
- Path Methods:
Files.path_get(): Retrieve the contents of a file or directory from a specified path.Files.path_post(): Upload or update a file at the given dest_path using contents from source.Files.path_delete(): Delete a file or directory at the specified path.
- Sharing Methods:
Files.sharing_post(): Enable sharing of a file from the given path (if not already shared) and get a link to it.Files.sharing_get(): Retrieve the sharing URL for a specified path.Files.sharing_delete(): Disable sharing for a specified path.
- Tree Method:
Files.tree_get(): Retrieve a list of regular files and subdirectories of a directory at the specified path (limited to 1000 results).
- path_delete(path: str) int[source]
Deletes the file at specified path (if file is a directory it will be deleted as well).
Returns 204 on sucess, raises otherwise.
- path_get(path: str) dict | bytes[source]
Returns dictionary of directory contents when path is an absolute path to of an existing directory or file contents if path is an absolute path to an existing file – both available to the PythonAnywhere user. Raises when path is invalid or unavailable.
- path_post(dest_path: str, content: bytes) int[source]
Uploads contents of content to dest_path which should be a valid absolute path of a file available to a PythonAnywhere user. If dest_path contains directories which don’t exist yet, they will be created.
Returns 200 if existing file on PythonAnywhere has been updated with source contents, or 201 if file from dest_path has been created with those contents.
- sharing_delete(path: str) int[source]
Stops sharing file at path.
Returns 204 on successful unshare.
- sharing_get(path: str) str[source]
Checks sharing status for a path.
Returns url with sharing link if file is shared or an empty string otherwise.
- sharing_post(path: str) Tuple[str, str][source]
Starts sharing a file at path.
Returns a tuple with a message and sharing link on success, raises otherwise. Message is “successfully shared” on success, “was already shared” if file has been already shared.
- tree_get(path: str) dict[source]
Returns list of absolute paths of regular files and subdirectories of a directory at path. Result is limited to 1000 items.
Raises if path does not point to an existing directory.
- tree_post(local_dir_path: str, remote_dir_path: str) None[source]
Uploads contents of a local directory to remote path on PythonAnywhere. Walks local_dir_path recursively and uploads each file using
path_post(), preserving directory structure.Raises
PythonAnywhereApiExceptionon first upload failure.