3
$\begingroup$

This is causing me lots of problems. I found that issuing SetDirectory[NotebookDirectory[]]; in notebook A.nb causes subsequent Directory[] command in a separate notebook B.nb and in different location to return the directory of A.nb (even though SetDirectory[NotebookDirectory[]]; has also been issued earlier in B.nb

This is a problem, since I have scripts that uses relative folder paths. And if I happen to open a second notebook and change the Directory there to run something else, then come back to the first notebook, my script does not work now since the Directory has changed, unless I remember to issue SetDirectory[NotebookDirectory[]]; each time, and to remember to do this again when I change to a different notebook. So I keep issuing SetDirectory[NotebookDirectory[]]; all the time now. This is so confusing as I am not sure which directory I am in at any moment.

Steps to reproduce:

  1. open a notebook in some folder, type SetDirectory[NotebookDirectory[]];
  2. open a notebook in different folder, type SetDirectory[NotebookDirectory[]];
  3. Go back to notebook in step (1) and type Directory[] you will see it has changed to whatever the notebook in (2) was in.

Is this a bug? Is this the expected behaviour? How can one make SetDirectory[NotebookDirectory[]]; not change the Directory[] of other open notebooks in different folders? Isn't the semantics of SetDirectory[NotebookDirectory[]] meant to affect only the current notebook and not all open notebooks?

If SetDirectory[NotebookDirectory[]] is meant to change the directory of all open notebooks, is there a solution to set the directory to the location of the current notebook, without having this global effect on all other notebooks?

I normally have few open notebooks, all in different places.

Version 10.1, windows 7

$\endgroup$

1 Answer 1

4
$\begingroup$

SetDirectory changes the kernels working directory, and as long as several notebooks share the same kernel, what you describe is what is expected. You could use different kernels for the different notebooks you have opened, but of course that is limited by kernel licenses and has many other consequences.

Alternatively you could use the CellProlog option to do a SetDirectory before every cell evaluation, probably defined in a stylesheet that your notebooks share. But that probably isn't very efficient and also quite obscure (I wouldn't want to search an error in such an environment...).

But honestly I think the problems you describe just show that it would be a good idea to not organize your code/scripts the way you do. I'd generally suggest to not rely on a specific working directory in any code you plan to reuse. If some code needs access to files, then either use full path information for the filenames or pass directory/search-path information as arguments.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.