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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With