Reputation: 189
I know that the directory can be automatically changed to that of the current script if we press F5.
But is there a way to automatically do so when I run the codes in interactive mode, or when I open a script? Currently I need to os.chdir() to the current working directory.
Thanks.
Upvotes: 3
Views: 1145
Reputation: 2701
You can add the following lines to your script:
import os
os.chdir(os.path.dirname(__file__))
__file__
will return the path of the script, and we can use os.path.dirname
to find which directory it is located in. Then just use os.chdir
to change to that directory.
Upvotes: 1