{"id":18330,"date":"2021-11-17T15:34:44","date_gmt":"2021-11-17T10:04:44","guid":{"rendered":"https:\/\/java2blog.com\/?p=18330"},"modified":"2021-11-17T15:34:47","modified_gmt":"2021-11-17T10:04:47","slug":"python-absolute-path","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-absolute-path\/","title":{"rendered":"Get Absolute Path in Python"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#What_is_an_absolute_path_to_a_file\">What is an absolute path to a file?<\/a><\/li><li><a href=\"#How_to_get_absolute_path_in_Python\">How to get absolute path in Python<\/a><ul><li><a href=\"#Use_abspath_to_get_the_absolute_path_to_file\">Use abspath to get the absolute path to file<\/a><\/li><li><a href=\"#use_pathlib_module_to_get_the_absolute_path_to_file\">use pathlib module to get the absolute path to file<\/a><\/li><\/ul><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n\n<p>We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative paths of files in our programs. In this article, we will discuss two ways to get the absolute path to a file in python from its relative pathname.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"What_is_an_absolute_path_to_a_file\">What is an absolute path to a file?<\/span><\/h2>\n\n\n\n<p>The absolute path to a file is the entire file path from the root file to the current file. For instance, Look at the following image.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/java2blog.com\/wp-content\/uploads\/2021\/11\/java2blogimage-800x449.png\" alt=\"\" class=\"wp-image-18331\" width=\"836\" height=\"469\" srcset=\"https:\/\/java2blog.com\/wp-content\/uploads\/2021\/11\/java2blogimage-800x449.png 800w, https:\/\/java2blog.com\/wp-content\/uploads\/2021\/11\/java2blogimage-300x169.png 300w, https:\/\/java2blog.com\/wp-content\/uploads\/2021\/11\/java2blogimage-768x431.png 768w, https:\/\/java2blog.com\/wp-content\/uploads\/2021\/11\/java2blogimage.png 1093w\" sizes=\"(max-width: 836px) 100vw, 836px\" \/><figcaption>file-path image<\/figcaption><\/figure>\n\n\n\n<p>You can observe that the <code>sample.txt<\/code> path is present in the <code>java2blog<\/code> folder. The <code>java2blog<\/code> folder is present in the <code>codes<\/code> folder.<\/p>\n\n\n\n<p>Here, the absolute path of the file <code>sample.txt<\/code> will always be <code>\u201c\/home\/aditya1117\/codes\/java2blog\/sample.txt\u201d<\/code>.&nbsp;<\/p>\n\n\n\n<p>On the other hand, its relative path will always change according to the folder from which we are viewing the file.&nbsp; For instance,&nbsp;<\/p>\n\n\n\n<ul><li>If we are currently in the <code>java2blog<\/code> folder, the relative path of the file will be <code>\/sample.<\/code>txt.<\/li><li>However, If we move to the folder <code>codes<\/code>, the relative path to the file <code>sample.txt<\/code> will be <code>\/java2blog\/sample.txt<\/code>.<\/li><\/ul>\n\n\n\n<p>So, the relative path to a file changes according to the folder we are currently in. Whereas, the absolute path to a file never changes until the file is moved to another location. We can access the file using the absolute path from any directory. Whereas, we will have to update the relative path if we move to any other directory even if the file is at the same location.<\/p>\n\n\n\n<p><h2><span id=\"How_to_get_absolute_path_in_Python\"> How to get absolute path in Python<\/span><\/h2>\n<p>Let us now look at the ways to get the absolute path to a file in python.<\/p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"Use_abspath_to_get_the_absolute_path_to_file\">Use abspath to get the absolute path to file <\/span><\/h3>\n\n\n\n<p>The <code>os<\/code> module in python provides us with the <code>abspath() <\/code>method under the <code>os.path<\/code> property. The <code>abspath()<\/code> method takes the relative path to a file and returns the absolute path in the string format as shown in the following example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\n\nrelative_path = 'sample.txt'\nabsolute_path = os.path.abspath(relative_path)\n\nprint(absolute_path)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/home\/aditya1117\/PycharmProjects\/pythonProject\/sample.txt<\/code><\/pre>\n\n\n\n<p>Here, the <code>sample.txt <\/code>file is present in the same folder as our source code file. So, the relative file has been given as only the file name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"use_pathlib_module_to_get_the_absolute_path_to_file\">use pathlib module to get the absolute path to file <\/span><\/h3>\n\n\n\n<p>Instead of using the os module, we can use the <code>pathlib<\/code> module to get the absolute path to a file in python. Here, we will use the <code>Path()<\/code> function and the <code>absolute()<\/code> method to get the absolute path to a file.<\/p>\n\n\n\n<p>The <code>Path()<\/code> function takes the file name as input and creates a path object.&nbsp; We can invoke the <code>absolute()<\/code> method on the path object to get the absolute path to a given file as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from pathlib import Path\n\nrelative_path = Path('sample.txt')\nabsolute_path = relative_path.absolute()\nprint(absolute_path)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/home\/aditya1117\/PycharmProjects\/pythonProject\/sample.txt\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n\n\n\n<p>In this article, we have discussed the relative path and the absolute path to a file. We have also discussed two approaches to get the absolute path in python. You can choose any of the approaches according to your preference as both the approaches have almost the same cost of execution.<\/p>\n\n\n\n<p>I hope you enjoyed reading this article. Stay tuned for more informative articles.<\/p>\n\n\n\n<p>Happy Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsWhat is an absolute path to a file?How to get absolute path in PythonUse abspath to get the absolute path to fileuse pathlib module to get the absolute path to fileConclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative paths of files [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":18381,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145,185],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18330"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=18330"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18330\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/18381"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=18330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=18330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=18330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}