How to find the directory of the .app that a python script is running from
<p>I recently made a python script that goes through files in whatever directory it is placed in and renames them based on certain criteria. The script works perfectly, and I compiled the script into an OS X .app using py2app. This worked fine as well. However now when I run the script, it searches through the files in the ".app/contents/macOS" folder (where the script is located) rather than where the ".app" is actually located.</p>

<p>This is because it has this code at the start:</p>

<pre><code>src = os.path.dirname(os.path.abspath(__file__))
</code></pre>

<p>which assigns the the location of the ".py" file to a variable which is then used extensively throughout the script. Is there any way I can instead add a snippet of code which tells python the path location of the ".app" that the ".py" file is executing from?</p>

<p>If not, perhaps there is a way to get a file explorer window open, from there it would be possible for a user to select a folder who's path would then get assigned to the "src" variable. I'm very new to python however so this would certainly be a challenge.</p>

<p>Try to use:</p>

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

<p>From docs:</p>

<blockquote>
  <p><strong>getcwd()</strong></p>
  
  <p>Return a unicode string representing the current working directory.</p>
</blockquote>

python osx python-3.x py2app