{"id":13218,"date":"2025-08-19T06:58:55","date_gmt":"2025-08-19T06:58:55","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13218"},"modified":"2025-08-19T06:58:55","modified_gmt":"2025-08-19T06:58:55","slug":"find-current-directory-and-files-directory","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/find-current-directory-and-files-directory","title":{"rendered":"Find the Current Directory and File&#8217;s Directory"},"content":{"rendered":"<p>Use os.getcwd() and __file__ with os.path or pathlib<br \/>\nTo determine both the current working directory and the script&#8217;s file location:<\/p>\n<h3>1. Get the current directory (where the script is run from):<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import os\r\ncwd = os.getcwd()\r\nprint(\"Current working directory:\", cwd)<\/pre>\n<h3>2. Get the directory where the Python file itself is located:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport os\r\nscript_dir = os.path.dirname(os.path.abspath(__file__))\r\nprint(\"Directory of the script file:\", script_dir)\r\n<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport os\r\nprint(\"Current working directory:\", os.getcwd())\r\nprint(\"Directory of this script file:\", os.path.dirname(os.path.abspath(__file__)))\r\n<\/pre>\n<h3>Using pathlib (Python 3.4+ alternative):<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nfrom pathlib import Path\r\ncwd = Path.cwd()\r\nscript_dir = Path(__file__).resolve().parent\r\nprint(\"Current working directory:\", cwd)\r\nprint(\"Directory of the script file:\", script_dir)\r\n<\/pre>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li>os.getcwd() gives you the directory where the Python process was started (the shell&#8217;s location).<\/li>\n<li>__file__ gives you the path of the script file being executed.<\/li>\n<li>Use Path(__file__).resolve().parent for a cleaner and more modern way with pathlib.<\/li>\n<\/ul>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/python-for-app-development\" target=\"_blank\">Python for App Development<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Use os.getcwd() and __file__ with os.path or pathlib To determine both the current working directory and the script&#8217;s file location: 1. Get the current directory (where the script is run from): import os cwd = os.getcwd() print(&#8220;Current working directory:&#8221;, cwd) 2. Get the directory where the Python file itself is located: import os script_dir = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13220,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-13218","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13218"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=13218"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13218\/revisions"}],"predecessor-version":[{"id":13221,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13218\/revisions\/13221"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13220"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}