Wolfram Research

Function Repository Resource:

EnsureExport

Source Notebook

Export data to a file, creating intermediate directories as necessary

Contributed by: Jesse Friedman

ResourceFunction["EnsureExport"]["path/dest.ext",expr]

exports data to a file, creating the directories in path if they don’t already exist.

ResourceFunction["EnsureExport"]["path/dest.ext",]

exports data to a file using the same arguments as Export.

Details and Options

ResourceFunction["EnsureExport"] accepts the same arguments and options as Export.

Examples

Basic Examples

Export data to a file in a directory that doesn’t already exist:

In[1]:=
ResourceFunction[
 "EnsureExport"]["~/thisdirectory/wontexist/untilthiscode/isevaluated.\
txt", "hello world"]
Out[1]=
In[2]:=
FileNames["~/thisdirectory/wontexist/untilthiscode/*"]
Out[2]=

Scope

EnsureExport accepts the same arguments and options as Export:

In[3]:=
list = Import["ExampleData/wave.gif"];
ResourceFunction[
 "EnsureExport"]["~/my/new/directory/animation.gif", list, "AnimationRepetitions" -> 2]
Out[4]=

When the depth of the specified path is zero, ExportToPath behaves exactly like Export:

In[5]:=
ResourceFunction["EnsureExport"]["test.png", Integrate[1/(x^4 - 1), x]]
Out[5]=

Properties and Relations

Export fails if the specified path is in a nonexistent directory:

In[6]:=
Export["~/thisdirectory/doesntexist/myfile.txt", "hello world"]
Out[6]=

EnsureExport creates directories that don’t exist:

In[7]:=
ResourceFunction[
 "EnsureExport"]["~/thisdirectory/doesntexist/myfile.txt", "hello \
world"]
Out[7]=

Possible Issues

EnsureExport, like Export, will fail if the current user has insufficient permissions to write to the specified path:

In[8]:=
ResourceFunction[
 "EnsureExport"]["/directory/myfile.txt", "hello world"]
Out[8]=

Resource History

Related Resources

License Information