0
$\begingroup$

I have some code, currently in a notebook, which should produce some output in the same directory as the notebook, for which I am using NotebookDirectory[]. I would also like to run this code in a .wls script file on a server, for which I believe I can get the script file directory using $InputFileName. However, I would like to have the same code in the notebook and the script file; is there some way to detect whether the current code is being run from a notebook or from a script, so that I can get the relevant file's directory in an agnostic way?

$\endgroup$
2
  • 2
    $\begingroup$ As per documentation for $InputFileName, this variable is empty ("") if the input does not come from a file. So you can check whether $InputFileName === "". $\endgroup$ Commented Aug 14 at 14:38
  • 1
    $\begingroup$ In addition to what @Domen mentioned, I tend to use $NotebookDirectory=DirectoryName[$InputFileName] in my .wls files for this purpose, with a Which clause to determine whether it's a script or a notebook. $\endgroup$ Commented Aug 14 at 18:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.