Working with Files#
Edge provides a highly scalable built-in storage space for files and folders. This is in addition to data accessed remotely with Data Connectors, and the local files managed in the Analysis App.
Files stored in Edge's internal storage can be accessed by multiple users in
your organization; they are also accessible in Jupyter notebooks via the
edge
built-in object. Note that while files are accessible to all users of
your organization, they cannot be viewed or modified by anyone outside of your
organization.
Browsing and opening files#
You can explore your files interactively using the Data App file viewer.
Files and folders can also be accessed programmatically via the edge
object automatically added to your Jupyter notebook. For example, to list
the contents of the root folder (top of the file tree), simply call
edge.files.list()
in a notebook cell.
Note
In this document, code preceded by >>>
indicates Python code that can be
included in a Python cell in a Jupyter Notebook. You run the code by
entering it into the cell without the >>>
and pressing Ctrl-Enter
on your keyboard or pressing the Run button in the toolbar.
Running this code list your files stored on Edge:
>>> edge.files.list()
['Example Folder',
'Experiments.txt',
'Imported Data',
'My Data',
'Projects',
'data.csv',
'data_cleaned.csv',
'image001.png',
'image002.png',
'image003.png']
You can also list just files with list_files()
or just folders with list_folders()
, avoiding the need
to process the results of list()
to determine which is which.
You can use this interface to open either files or folders. Here, we open the "Imported Data" folder and assign it to a variable in the notebook. Opening a folder returns a Folder object:
>>> imported_folder = edge.files.open("Imported Data")
>>> type(imported_folder)
edge.api.Folder
You can list the contents Listing the contents of this subfolder shows the files it contains:
>>> imported_folder.list()
['datacapture001.png', 'datacapture002.png']
This lets you explore the file tree step-by-step, by opening folders and subfolders in turn.
Of course, you can open an individual file, too. What do you get in return? That depends on the file type. For example, if we open the "data.csv" file in the root folder, Edge will automatically convert it to a Pandas dataframe for us:
>>> data = edge.files.open("data.csv")
>>> type(data)
pandas.core.frame.DataFrame
Likewise, if we open an image file, we get a Python Imaging Library (PIL) image object:
>>> data = edge.files.open("image001.png")
>>> type(data)
PIL.PngImagePlugin.PngImageFile
Finally, if we just want the bytes of the file, we can get a raw "file-like" object by requesting that Edge open the file with a particular handler:
>>> data = edge.files.open("image001.png", open_with="file")
>>> type(data)
edge.api.File
>>> databytes = data.read()
>>> type(databytes)
bytes
Downloading a file#
The simplest way to download a file to your desktop is to use the
Data App. You can also download a file via the Edge API.
Every Folder
object in Edge has a download()
method. Just specify the
name of the file you want to retrieve:
>>> edge.files.download('image002.png')
If you list the contents of your Analysis App home directory, you'll see the downloaded file show up:
$ ls
MyNotebook.ipynb image002.png
Note
Running the edge.files.download()
function in a Jupyter Notebook in the
Analysis App will download the file into the home directory of your
Analysis App. If you wish to download files locally, use the Data App
or a local instance of the EdgeSession
object from the enthought_edge
Python package, available from the Enthought Deployment Manager.
Uploading Files to Edge#
The simplest way to upload a file from your desktop to Edge is by dragging and
dropping it into the Data App file browser.
You can also upload files programmatically, using edge.files
. Here's an
example. Suppose you have a file called mydata.csv
in your Analysis App
local file tree. Then, to upload it to the root folder in Edge's internal
file storage, you would open the file and pass it to edge.files.upload
:
>>> with open('mydata.csv', 'rb') as myfile:
... edge.files.upload('mydata.csv', myfile)
Note
If a file by that name already exists, an edge.exceptions.AlreadyExists
exception is raised.
Other file management operations#
In the right-click menus for the Data App file browser, you'll find multiple options to move or rename files & folders, in addition to the upload/download options described above. These also have counterparts in the Edge Python API.
For example, we can make a new folder in the root by calling make_folder
:
>>> edge.files.make_folder('newfolder')
If we don't like the name of a file or folder, we can change it:
>>> edge.files.rename('newfolder', 'New Folder')
We can even move it into another folder:
>>> edge.files.move('New Folder', 'Imported Data/New Folder')
The destination of a move is relative to the Folder
object whose move()
method is being called. When moving,
the destination is an optional path and a file or folder name. The difference between rename()
and
move()
is the ability to specify a new subfolder (or subfolders) during a move()
.
And we can delete it:
>>> edge.files.delete('Imported Data/New Folder')
The delete operation will fail on a folder with an edge.exceptions.FolderNotEmpty
exception if you try to delete
a folder that is not empty.
Names and Paths#
For every Folder method except rename()
, the file names can be specified as posix-style paths. Prefixing the
path with /
makes the path absolute from the root of the file system, otherwise the path is relative to the
Folder
that the operation is performed with. Posix operators .
and ..
are both honored.