Working with the file system¶
The Workspaces file system¶
The files shown in the left-hand pane of the Workspaces environment represent the contents of your Workspace directory, which is the notebook’s working directory.
To view the full path, run pwd or the following command:
import os
print(os.getcwd())
This prints a path in the following format:
/workspace/<workspace_hash representing your workspace>
Using ls to list files displays notebook files and any folders or project assets stored in the Workspace directory.
Referencing files¶
You can reference files in the current Workspace directory by both the full path and relative path. For example, you want to read in a notebook and a sample dataset (in CSV) that are in your workspace:
ml-intent-prediction/
├── data/
│ └── sample_data.csv
├── notebooks/
│ └── analysis.ipynb
└── utilities.py
Read data using the relative path:
import pandas as pd
df = pd.read_csv("../data/sample_data.csv")
df.head()
Read data using the full path:
import pandas as pd
df = pd.read_csv("/workspace/<workspace_hash>/ml-intent-prediction/data/sample_data.csv")
df.head()
Limitations¶
Writing files to the Workspace directory from code or the terminal is not supported. While file writes may appear to work during a session, they are not guaranteed to succeed and may fail in future releases.
File persistence in the Workspace directory has the following limitations:
Files are read-only: Files under
/workspace/<workspace_hash>are read-only and cannot be updated in code while executing the notebook.File writes from code or terminal are not supported: Do not write files to the Workspace directory programmatically. Use Snowflake stages instead for persisting files (see Persisting files).
Only files uploaded or created in Snowsight persist: Only files that are uploaded or created through Snowsight persist across sessions.
Session-only visibility: Any files created from code or the terminal during a session are removed when the notebook service is suspended. These files do not appear in the left-hand pane.
The /tmp directory of the container¶
The /tmp directory is also read/write and is suitable for scratch work or temporary data that does not need to persist.
An example of writing a file to /tmp:
file_path = "/tmp/sample.txt"
with open(file_path, "w") as f:
f.write("Hello from Python!\\nThis is a sample file saved in /tmp.")
print(f"File written to {file_path}")
To list files in the /tmp directory, run the following:
%%bash
cd /tmp
ls
Persisting files¶
To store files for later use, write them to a Snowflake stage with write access using Snowpark file operation APIs.
To learn more about required stage privileges, see write access. For Snowpark file operations, see Snowpark file operation APIs.