{"id":467,"date":"2016-03-15T19:43:46","date_gmt":"2016-03-15T11:43:46","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=467"},"modified":"2018-09-04T22:56:41","modified_gmt":"2018-09-04T14:56:41","slug":"how-to-get-the-directory-path-of-a-python-3-script-from-within-itself","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-get-the-directory-path-of-a-python-3-script-from-within-itself\/","title":{"rendered":"How to get the directory path of a Python 3 script from within itself"},"content":{"rendered":"<p>One of the best way to reference files in Python scripts is to based the file path on the directory where the Python script resides in. This will allow us to always reference our files correctly no matter where our Python script is being executed.<\/p>\n<p>This post documents the Python 3 code that I often used to get the directory path of a Python 3 script from within itself.<\/p>\n<h3>Python 3 code to get the directory path of the running script<\/h3>\n<p>To get the directory path of a Python 3 script, I will typically write the following code: <\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nscriptDirectory = os.path.dirname(os.path.realpath(__file__))\r\n<\/pre>\n<p>I will first import the <code>os<\/code> module. With the <code><a href=\"https:\/\/docs.python.org\/3\/library\/os.html\" title=\"Python 3 reference for os module\" target=\"_blank\">os<\/a><\/code> module imported, I can then use the <code><a href=\"https:\/\/docs.python.org\/3\/library\/os.path.html#os.path.realpath\" title=\"Python 3 reference for os.path.realpath function\" target=\"_blank\">realpath<\/a><\/code> function of the <code><a href=\"https:\/\/docs.python.org\/3\/library\/os.path.html\" title=\"Python 3 reference for os.path module\" target=\"_blank\">os.path<\/a><\/code> module on the <code><a href=\"https:\/\/docs.python.org\/3\/reference\/import.html#__file__\" title=\"Python 3 reference for __file__ attribute\" target=\"_blank\">__file__<\/a><\/code> attribute to get the file path of the current Python 3 script. I then use the <code><a href=\"https:\/\/docs.python.org\/3\/library\/os.path.html#os.path.dirname\" title=\"Python 3 reference for the os.path.dirname function\" target=\"_blank\">dirname<\/a><\/code> function to get the directory where the current Python 3 script resides on. <\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-7x\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-7x&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-7x&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F467&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>One of the best way to reference files in Python scripts is to based the file path on the directory where the Python script resides in. This will allow us to always reference our files correctly no matter where our Python script is being executed.<\/p>\n<p>This post documents the Python 3 code that I often used to get the directory path of a Python 3 script from within itself.<\/p>\n","protected":false},"author":1,"featured_media":1244,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[234,226,233],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-7x","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/467"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=467"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/467\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1244"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}