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 (1) 

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 (2) 

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 (2) 

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 (1) 

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]=

Publisher

Jesse Friedman

Version History

  • 1.0.0 – 14 October 2019

Related Resources

License Information