{"id":6418,"date":"2021-05-20T09:42:47","date_gmt":"2021-05-20T09:42:47","guid":{"rendered":"https:\/\/linuxways.net\/?p=6418"},"modified":"2021-05-20T09:42:47","modified_gmt":"2021-05-20T09:42:47","slug":"python-get-current-directory","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/python-get-current-directory\/","title":{"rendered":"Python Get Current Directory"},"content":{"rendered":"<p>When working with python files in directories, it is recommended to use absolute file paths. However, if you are dealing with the relative paths then, you must have a better understanding of the current working directory and how to get the current directory in python. The absolute and relative paths are different from each other. The relative path always begins from the current directory, while the absolute path represents a file directory location that starts from the root directory.<\/p>\n<p>To get a current working directory, the os python module is used that helps to interact with the operating system. This module is a part of the standard python library and includes all the methods required to find the current working directory.<\/p>\n<p>We will go through in this tutorial how to get the current python directory.<\/p>\n<h2><strong>Get current directory in Python using os.getcwd() method<\/strong><\/h2>\n<p>In order to get the python current directory via the terminal, Open the terminal using the \u2018Ctrl+Alt+t\u2019 keys and make sure that python should be installed on your system. Now, import the os python module by using the following command:<\/p>\n<pre>&gt;&gt;&gt; import os<\/pre>\n<p>Use the getcwd() method that returns a string that prints the absolute path of the current directory. The returned string will not show the slash characters.<\/p>\n<pre>&gt;&gt;&gt; os.getcwd()<\/pre>\n<p>The following output shows on the terminal window if you get python current directory path through the terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1088\" height=\"269\" class=\"wp-image-6419\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-100.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-100.png 1088w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-100-300x74.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-100-1024x253.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-100-768x190.png 768w\" sizes=\"auto, (max-width: 1088px) 100vw, 1088px\" \/><\/p>\n<p><strong>Example<\/strong><\/p>\n<p>Let\u2019s explain the above method with the help of an example which is mentioned below:<\/p>\n<p>Create a python script file. Open an empty text file and paste the following source code in this file:<\/p>\n<pre># Import os module\r\n\r\nimport os\r\n\r\n# Get current python directory\r\n\r\ncwd = os.getcwd()\r\n\r\n# display current python directory\r\n\r\nprint(\"Current working directory: {0}\".format(cwd))\r\n\r\n# Display the returned object type\r\n\r\nprint(\"os.getcwd() returns an object of type: {0}\".format(type(cwd)))<\/pre>\n<p>Now, save the above file with the \u2018.py\u2019 extension with some suitable name.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1090\" height=\"428\" class=\"wp-image-6420\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-101.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-101.png 1090w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-101-300x118.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-101-1024x402.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-101-768x302.png 768w\" sizes=\"auto, (max-width: 1090px) 100vw, 1090px\" \/><\/p>\n<p>In the above source code, the os module is imported at the top of the file, and then using this module gets the current directory path. Execute this python file through the terminal by running the following command:<\/p>\n<pre>$ python file-name.py<\/pre>\n<p>Here, the file name is \u2018getdirectory.py\u2019. So, change the above command into the following form:<\/p>\n<pre>$ python getdirectory.py<\/pre>\n<p>The following python current directory shows on the terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1085\" height=\"133\" class=\"wp-image-6421\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-102.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-102.png 1085w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-102-300x37.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-102-1024x126.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-102-768x94.png 768w\" sizes=\"auto, (max-width: 1085px) 100vw, 1085px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>We have studied in this article, by using the \u2018os. getcwd()\u2019 method you can easily get the python current working directory. We have also mentioned an example to explain the current directory concept. Moreover, you can also explore more uses of the os.getcwd() method through internet resources. I hope the above-explained information will provide you a complete guide to get the python current working directory.<\/p>","protected":false},"excerpt":{"rendered":"<p>When working with python files in directories, it is recommended to use absolute file paths. However, if you are dealing with the relative paths then, you must have&hellip;<\/p>","protected":false},"author":1,"featured_media":6446,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[10],"class_list":["post-6418","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/6418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=6418"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/6418\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/6446"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=6418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=6418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=6418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}