Hsun-Yi Hsieh Hsun-Yi Hsieh - 4 months ago 15
Python Question

Find the right path with os.chdir() in digital ocean

I tried several ways but never succeeded. I need to change the directory in digital ocean (

/home/vagrant
). I have files saved in the same folder as where the file.py file is. file.py and other files that file.py aim to execute are all in
/folder/PH/Programs
. When I ran
$python file.py
, it appears an error of the directory:

os.chdir('/folder/PH/Programs')


OSError: [Errno 2] No such file or directory: '/folder/PH/Programs'


I am sorry for this naive question. Thank you.

Answer

Either you provide the full path to file, or do not put a slash at the beginning. That means the root directory.

go with:

os.chdir('~/folder/PH/Programs')

If it is in your home directory, or otherwise specify the full path to file.

(In fact, the path you say in your question is wrong for the same reason).

Comments