How to go one or more levels using os.chdir with '..''s as arguments
<p>How do I change path in Python using '..' to go down one or more levels as argument to os.chdir(). So that if I am on /home/usr/one and I want to go to 'home' directory a '../..' argument to chdir will do it. Do I wrap the argument in some other function? </p>

<p>As you say in your question, if you are in the directory <code>/home/usr/one</code> <code>os.chdir('../../')</code> will bring you to <code>/home/</code>. </p>

<p>You can confirm this by calling:</p>

<pre><code>os.getcwd()
</code></pre>

<p>Before and after changing directories. This function will show you the current working directory. Also, there is no need to wrap the argument to <code>chdir()</code> in another function.</p>

<h1>Edit:</h1>

<p>Note that <code>os.chdir()</code> in a script will not change the directory you are in when you run the script from the terminal. In other words, if you are in <code>/home/usr/one</code> and run a script with <code>python myscript.py</code>, any directory changes made with <code>os.chdir()</code> within that script will not be refelected when the script finishes; you will still be in <code>/home/usr/one</code>.</p>

python python-2.7 python-3.x