How do I get the path and name of the file that is currently executing?
<p>I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process. </p>

<p>For example, let's say I have three files. Using <a href="http://docs.python.org/library/functions.html#execfile">execfile</a>:</p>

<ul>
<li><code>script_1.py</code> calls <code>script_2.py</code>. </li>
<li>In turn, <code>script_2.py</code> calls <code>script_3.py</code>. </li>
</ul>

<p>How can I get the file name and path of <strong><code>script_3.py</code></strong>, <em>from code within <code>script_3.py</code></em>, without having to pass that information as arguments from <code>script_2.py</code>?</p>

<p>(Executing <code>os.getcwd()</code> returns the original starting script's filepath not the current file's.)</p>

<pre><code>__file__
</code></pre>

<p>as others have said. You may want to use: </p>

<pre><code>os.path.realpath(__file__)
</code></pre>
