<p>Given a path such as <code>&quot;mydir/myfile.txt&quot;</code>, how do I find the file's absolute path in Python? E.g. on Windows, I might end up with:</p>
<pre><code>&quot;C:/example/cwd/mydir/myfile.txt&quot;
</code></pre>

[{'answer_id': 51523, 'author': 'sherbang', 'author_id': 5026, 'author_profile': 'https://Stackoverflow.com/users/5026', 'pm_score': 11, 'selected': True, 'text': '<pre><code>&gt;&gt;&gt; import os\n&gt;&gt;&gt; os.path.abspath("mydir/myfile.txt")\n\'C:/example/cwd/mydir/myfile.txt\'\n</code></pre>\n\n<p>Also works if it is already an absolute path:</p>\n\n<pre><code>&gt;&gt;&gt; import os\n&gt;&gt;&gt; os.path.abspath("C:/example/cwd/mydir/myfile.txt")\n\'C:/example/cwd/mydir/myfile.txt\'\n</code></pre>\n'}, {'answer_id': 58417, 'author': 'Tom', 'author_id': 3715, 'author_profile': 'https://Stackoverflow.com/users/3715', 'pm_score': 5, 'selected': False, 'text': '<p>Install a third-party <a href="https://github.com/jaraco/path" rel="noreferrer">path module</a> (found on <a href="https://pypi.org/project/path/" rel="noreferrer"><code>PyPI</code></a>), it wraps all the <code>os.path</code> functions and other related functions into methods on an object that can be used wherever strings are used:</p>\n<pre><code>&gt;&gt;&gt; from path import path\n&gt;&gt;&gt; path(\'mydir/myfile.txt\').abspath()\n\'C:\\\\example\\\\cwd\\\\mydir\\\\myfile.txt\'\n</code></pre>\n'}, {'answer_id': 15325066, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>Today you can also use the <code>unipath</code> package which was based on <code>path.py</code>: <a href="http://sluggo.scrapping.cc/python/unipath/" rel="noreferrer">http://sluggo.scrapping.cc/python/unipath/</a></p>\n\n<pre><code>&gt;&gt;&gt; from unipath import Path\n&gt;&gt;&gt; absolute_path = Path(\'mydir/myfile.txt\').absolute()\nPath(\'C:\\\\example\\\\cwd\\\\mydir\\\\myfile.txt\')\n&gt;&gt;&gt; str(absolute_path)\nC:\\\\example\\\\cwd\\\\mydir\\\\myfile.txt\n&gt;&gt;&gt;\n</code></pre>\n\n<p>I would recommend using this package as it offers <a href="http://sluggo.scrapping.cc/python/unipath/Unipath-current/README.html" rel="noreferrer">a clean interface to common os.path utilities</a>.</p>\n'}, {'answer_id': 26539947, 'author': 'twasbrillig', 'author_id': 2213647, 'author_profile': 'https://Stackoverflow.com/users/2213647', 'pm_score': 7, 'selected': False, 'text': '<p>You could use the new Python 3.4 library <code>pathlib</code>. (You can also get it for Python 2.6 or 2.7 using <code>pip install pathlib</code>.) The authors <a href="http://www.python.org/dev/peps/pep-0428/#abstract">wrote</a>: "The aim of this library is to provide a simple hierarchy of classes to handle filesystem paths and the common operations users do over them."</p>\n\n<p>To get an absolute path in Windows:</p>\n\n<pre><code>&gt;&gt;&gt; from pathlib import Path\n&gt;&gt;&gt; p = Path("pythonw.exe").resolve()\n&gt;&gt;&gt; p\nWindowsPath(\'C:/Python27/pythonw.exe\')\n&gt;&gt;&gt; str(p)\n\'C:\\\\Python27\\\\pythonw.exe\'\n</code></pre>\n\n<p>Or on UNIX:</p>\n\n<pre><code>&gt;&gt;&gt; from pathlib import Path\n&gt;&gt;&gt; p = Path("python3.4").resolve()\n&gt;&gt;&gt; p\nPosixPath(\'/opt/python3/bin/python3.4\')\n&gt;&gt;&gt; str(p)\n\'/opt/python3/bin/python3.4\'\n</code></pre>\n\n<p>Docs are here: <a href="https://docs.python.org/3/library/pathlib.html">https://docs.python.org/3/library/pathlib.html</a></p>\n'}, {'answer_id': 49639219, 'author': 'chikwapuro', 'author_id': 4954966, 'author_profile': 'https://Stackoverflow.com/users/4954966', 'pm_score': 2, 'selected': False, 'text': '<p>if you are on a mac </p>\n\n<pre><code>import os\nupload_folder = os.path.abspath("static/img/users")\n</code></pre>\n\n<p>this will give you a full path:</p>\n\n<pre><code>print(upload_folder)\n</code></pre>\n\n<p>will show the following path:</p>\n\n<pre><code>&gt;&gt;&gt;/Users/myUsername/PycharmProjects/OBS/static/img/user\n</code></pre>\n'}, {'answer_id': 51179697, 'author': 'BND', 'author_id': 4088081, 'author_profile': 'https://Stackoverflow.com/users/4088081', 'pm_score': 0, 'selected': False, 'text': '<p>In case someone is using python and linux and looking for full path to file:</p>\n\n<pre><code>&gt;&gt;&gt; path=os.popen("readlink -f file").read()\n&gt;&gt;&gt; print path\nabs/path/to/file\n</code></pre>\n'}, {'answer_id': 53950650, 'author': 'Mad Physicist', 'author_id': 2988730, 'author_profile': 'https://Stackoverflow.com/users/2988730', 'pm_score': 5, 'selected': False, 'text': '<p>Update for Python 3.4+ <a href="https://docs.python.org/3/library/pathlib.html" rel="noreferrer"><code>pathlib</code></a> that actually answers the question:</p>\n\n<pre><code>from pathlib import Path\n\nrelative = Path("mydir/myfile.txt")\nabsolute = relative.absolute()  # absolute is a Path object\n</code></pre>\n\n<p>If you only need a temporary string, keep in mind that you can use <code>Path</code> objects with all the relevant functions in <a href="https://docs.python.org/3/library/os.path.html" rel="noreferrer"><code>os.path</code></a>, including of course <a href="https://docs.python.org/3/library/os.path.html#os.path.abspath" rel="noreferrer"><code>abspath</code></a>:</p>\n\n<pre><code>from os.path import abspath\n\nabsolute = abspath(relative)  # absolute is a str object\n</code></pre>\n'}, {'answer_id': 54929069, 'author': 'Lucas Azevedo', 'author_id': 4075155, 'author_profile': 'https://Stackoverflow.com/users/4075155', 'pm_score': 4, 'selected': False, 'text': '<p>This <strong>always</strong> gets the right filename of the current script, even when it is called from within another script. It is especially useful when using <code>subprocess</code>.</p>\n\n<pre><code>import sys,os\n\nfilename = sys.argv[0]\n</code></pre>\n\n<p>from there, you can get the script\'s full path with:</p>\n\n<pre><code>&gt;&gt;&gt; os.path.abspath(filename)\n\'/foo/bar/script.py\'\n</code></pre>\n\n<p>It also makes easier to navigate folders by just appending <code>/..</code> as many times as you want to go \'up\' in the directories\' hierarchy. </p>\n\n<p>To get the cwd:</p>\n\n<pre><code>&gt;&gt;&gt; os.path.abspath(filename+"/..")\n\'/foo/bar\'\n</code></pre>\n\n<p>For the parent path:</p>\n\n<pre><code>&gt;&gt;&gt; os.path.abspath(filename+"/../..")\n\'/foo\'\n</code></pre>\n\n<p>By combining <code>"/.."</code> with other filenames, you can access any file in the system.</p>\n'}, {'answer_id': 55034470, 'author': 'benjimin', 'author_id': 5104777, 'author_profile': 'https://Stackoverflow.com/users/5104777', 'pm_score': 5, 'selected': False, 'text': "<pre><code>import os\nos.path.abspath(os.path.expanduser(os.path.expandvars(PathNameString)))\n</code></pre>\n\n<p>Note that <code>expanduser</code> is necessary (on Unix) in case the given expression for the file (or directory) name and location may contain a leading <code>~/</code>(the tilde refers to the user's home directory), and <code>expandvars</code> takes care of any other environment variables (like <code>$HOME</code>).</p>\n"}, {'answer_id': 68986016, 'author': 'Dhiaa Shalabi', 'author_id': 11795918, 'author_profile': 'https://Stackoverflow.com/users/11795918', 'pm_score': 3, 'selected': False, 'text': "<p>You can use this to get absolute path of a specific file.</p>\n<pre><code>from pathlib import Path\n\nfpath = Path('myfile.txt').absolute()\n\nprint(fpath)\n</code></pre>\n"}, {'answer_id': 71029352, 'author': 'Evan Carroll', 'author_id': 124486, 'author_profile': 'https://Stackoverflow.com/users/124486', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n<p>Given a path such as <code>mydir/myfile.txt</code>, how do I find the file\'s absolute path relative to the current working directory in Python?</p>\n</blockquote>\n<p>I would do it like this,</p>\n<pre class="lang-py prettyprint-override"><code>import os.path\nos.path.join( os.getcwd(), \'mydir/myfile.txt\' )\n</code></pre>\n<p>That returns <code>\'/home/ecarroll/mydir/myfile.txt\'</code></p>\n'}]
['https://Stackoverflow.com/questions/51520', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/974/']