# Why isn't the default directory of savefig() the folder where the file is?

**URL:** <https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847>\
**Category:** Visualization\
**Tags:** pyplot\
**Created:** [December 10, 2016, 4:26pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847 "2016-12-10T16:26:27Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 10, 2016, 4:26pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/1 "2016-12-10T16:26:28Z")

</div>

Hello,

I expected the default directory for saving a figure is the same folder where the file is and found that this is not the case. So I have to specify a directory each time and it’s not very convenient for other users of my code when they run my code on their own machines. Is there a way to set the default directory? If not, how can I get the directory where the file is saved? Then I can change directory in a relative way.

Sorry if this is a silly question or doesn’t make much sense. Thanks!!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 10, 2016, 5:33pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/2 "2016-12-10T17:33:17Z")

</div>

What visualization package are you using?

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 10, 2016, 5:48pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/3 "2016-12-10T17:48:13Z")

</div>

Sorry. I’m using pyplot.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 10, 2016, 6:25pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/4 "2016-12-10T18:25:16Z")

</div>

It’s not specific to any plotting packages or any file operations in general. Almost all file operations takes relative paths that are relative to the current directory, which has nothing to do with the current file being executed. The only exception I can think of is `include`, which uses the current toplevel file path since it’s more useful.

If you start julia from command line, the current directory should already be the directory you are in before starting julia. Otherwise (if you are on windows and clicked the icon to start julia) you can just `cd` into the right directory.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 10, 2016, 6:35pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/5 "2016-12-10T18:35:43Z")

</div>

I understand this is the case that the current directory is the directory pwd() returns in Julia terminal. But I don’t think this is very convenient. On the one hand, when one has all his/her files for a project in a folder, shouldn’t s/he expect the figure generated to be saved in the same folder for this project, not anywhere else. On the other hand, when an absolute path has to be given for a file to be saved in a place, the file is then not very portable. This is also quite irritating when people work on the same code for collaboration. That’s why I’m looking for an easiest way to specify a relative path.

Thanks!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 10, 2016, 6:49pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/6 "2016-12-10T18:49:08Z")

</div>

> [@chobbes](#):
>
> On the one hand, when one has all his/her files for a project in a folder, shouldn’t s/he expect the figure generated to be saved in the same folder for this project, not anywhere else

No. The location of the data has nothing to do with the location of the code, especially not with the top level file currently being evaluated. We provide `@ __FILE__ ` and `@ __DIR__ ` when you have a script that operate only on a project specific hard coded relative path.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [December 10, 2016, 7:43pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/7 "2016-12-10T19:43:49Z")

</div>

If you use Juno, you can specify the current work directory to be set to the current file’s folder, using the Julia dropdown menu. It will set the work directory thus automatically if you open Atom by opening a .jl file.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 10, 2016, 9:14pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/8 "2016-12-10T21:14:01Z")

</div>

> No. The location of the data has nothing to do with the location of the code, especially not with the top level file currently being evaluated. We provide @ **FILE** and @ **DIR** when you have a script that operate only on a project specific hard coded relative path.

Thanks for the comments. I think this is all about the idealogy of design. For example, Matlab creates figures and save them in the directory where the file is. Could you please give me a concrete example for @ **FILE** () and @ **DIR** (). I don’t quite understand the explanation on [http://docs.julialang.org/en/release-0.5/stdlib/file/](http://docs.julialang.org/en/release-0.5/stdlib/file/)

Thanks!

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 10, 2016, 9:16pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/9 "2016-12-10T21:16:47Z")

</div>

Thanks for the hint. It works amazingly with Atom.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [December 10, 2016, 11:11pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/10 "2016-12-10T23:11:32Z")

</div>

e.g. put `cd(dirname(@ __FILE__ ()))` at the top of your file

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 11, 2016, 10:03am UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/11 "2016-12-11T10:03:56Z")

</div>

Thanks a lot for the example. It works perfectly for my needs.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 11, 2016, 10:11am UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/12 "2016-12-11T10:11:48Z")

</div>

Just to confirm - @ **DIR** wasn’t included in 5.0, was it?

Could anyone let me know the difference between PROGRAM\_FILE and @ **FILE**? I added `println(dirname(PROGRAM_FILE))` in a file and ran the file. But it doesn’t give me anything at the terminal.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 11, 2016, 10:30pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/13 "2016-12-11T22:30:48Z")

</div>

> [@chobbes](#):
>
> For example, Matlab creates figures and save them in the directory where the file is.

No, Matlab interprets relative paths with respect to the current working directory, just like Julia (and Python, I believe).

And rightly so. Now, if I want to do similar analysis on data in a different folder, is simply `cd` into the folder and rerun my code, confident that data will be saved to the correct relative position. It would be very inconvenient if I had to actually copy the code into each folder to achieve the same effect.

(There are of course multiple ways to achieve this, but I strongly support the _status quo_ here.)

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 12, 2016, 11:14am UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/14 "2016-12-12T11:14:45Z")

</div>

```julia
No, Matlab interprets relative paths with respect to the current working directory, just like Julia (and Python, I believe).

```

Well, you’re right. But note that each time you run a file, the current directory is automatically changed to the directory where the file is saved no matter where the working directory was. It’s equivalent to having the current directory sticking to the folder of the file.

I’m set now with Julia’s setting as I know how to change directory relatively. Thank you!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 12, 2016, 11:35am UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/15 "2016-12-12T11:35:59Z")

</div>

> [@chobbes](#):
>
> But note that each time you run a file, the current directory is automatically changed to the directory where the file is saved no matter where the working directory was.

This is not correct. Only if the file you are trying to run _is not in the search path_, Matlab will ask you whether you want to add it to the path or change the working path to the same path as where the file is located.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 12, 2016, 1:09pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/16 "2016-12-12T13:09:22Z")

</div>

> [@chobbes](#):
>
> I’m set now with Julia’s setting as I know how to change directory relatively.

Are you really sure this is the wisest course of action?

Changing your current working directory for each set of _data_ can make a lot of sense. Changing it to the location of your _code_ is a lot more questionable, in my opinion.

Remember, it is likely that you will run the same code on many different datasets. Will you move the code around to match the location of your data? Isn’t it better to let your code work relative to whatever is the current working directory, and leave it up to the user to change directory for each dataset? ( **Edit:** I guess you could move your data to the location of your code, but that too seems awkward to me.)

I know that I, as a user, certainly do _not_ appreciate it when scripts start changing my working directory. I suggest that you at least automatically change the directory _back_ after the script has finished running.

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 12, 2016, 3:21pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/17 "2016-12-12T15:21:05Z")

</div>

> This is not correct. Only if the file you are trying to run is not in the search path, Matlab will ask you whether you want to add it to the path or change the working path to the same path as where the file is located.

Yes, you’re right. Does Julia change the current directory if the file that I’m running is not in the paths? I guess not and this will remain not in the future?

> Changing your current working directory for each set of data can make a lot of sense. Changing it to the location of your code is a lot more questionable, in my opinion.

> Remember, it is likely that you will run the same code on many different datasets. Will you move the code around to match the location of your data? Isn’t it better to let your code work relative to whatever is the current working directory, and leave it up to the user to change directory for each dataset? (Edit: I guess you could move your data to the location of your code, but that too seems awkward to me.)

You’re absolutely right when you have many sets of data located in different places and they’re to be processed by a same file. However, I’m collaborating with others on a paper using GIT and the Julia code and the TeX files are located in two sub-folders of the main directory of this project. In other words, the relative location of these two sub-folders are fixed. We wish the figures generated by our Julia code to be saved in the sub-folder where our TeX files are. Then after my collaborators check out the updates each time, they can run the code straight away, expecting the figures are saved in the correct location that they can compile the tex file without relocating the figures. That’s why I need relative locations in this case. Hope that I have made my case understandable.

> I know that I, as a user, certainly do not appreciate it when scripts start changing my working directory. I suggest that you at least automatically change the directory back after the script has finished running.

This is a good point. Now I add a line at the end of my code to change my directory back after saving the figures in the desired location.

Thank you!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 12, 2016, 4:38pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/18 "2016-12-12T16:38:36Z")

</div>

Changing working directory only affects the current process, so there’s no need for this: a Julia script can only affect its own working directory, not that of its parent process. (This is why `cd` must be a shell built-in and cannot be a normal program.)

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 17, 2016, 3:20pm UTC](https://discourse.julialang.org/t/why-isnt-the-default-directory-of-savefig-the-folder-where-the-file-is/847/19 "2016-12-17T15:20:21Z")

</div>

Fair enough. Point taken.

Sorry. Didn’t see your comment. Thanks a lot!
