Directories in Wolfram Language
Here’s an idiomatic Wolfram Language example that demonstrates working with directories:
This Wolfram Language code demonstrates various operations for working with directories:
We use
CreateDirectory
to create new directories.The
createEmptyFile
function usesExport
to create empty files.FileNames
is used to list directory contents. When called without arguments, it lists the current directory.SetDirectory
changes the current working directory, similar tocd
in shell commands.ResetDirectory
returns to the previous working directory.FileNames
with theInfinity
option recursively lists all files and subdirectories.DeleteDirectory
with theDeleteContents -> True
option removes a directory and all its contents.
To run this code:
- Copy the code into a notebook in Wolfram Desktop or Mathematica.
- Evaluate the entire notebook or individual cells.
The output will show the directory listings and the recursive visit of the created directory structure.
Note that Wolfram Language provides high-level functions for file and directory operations, making it easy to work with the file system in a platform-independent manner. Unlike languages that require explicit error handling, Wolfram Language will automatically throw exceptions if operations fail, which can be caught using Check
or Quiet
if needed.