Get directory dynamically with python script
<p>I run my python script in the background from a terminal with:</p>

<pre><code>python myscript.py &amp;
</code></pre>

<p>In the script I have a loop which gets the current directory with os.getcwd(). If I change my working directory in the terminal though, the script doesn't get the new directory because as far as I have understood the script is attached to the original directory from which it was launched. </p>

<p>How can I update the current directory from a python script, i.e. how can I keep track of the current working directory of the process that launched the script?</p>

<p><em>Disclaimer:</em>  don't do this.  </p>

<pre><code>import os
import subprocess
from time import sleep

ppid = os.getppid()

print "parent process id: ", ppid
subprocess.check_call(['pwdx', str(ppid)])
sleep(5)  # do `cd other` in the parent process here
subprocess.check_call(['pwdx', str(ppid)])
</code></pre>

python terminal directory