{"id":3089,"date":"2021-04-21T07:39:12","date_gmt":"2021-04-21T02:09:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=3089"},"modified":"2021-11-22T18:45:03","modified_gmt":"2021-11-22T13:15:03","slug":"how-to-change-current-working-directory-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/","title":{"rendered":"How to change current working directory in python ?"},"content":{"rendered":"

How to get and change current working directory in python ?<\/h2>\n

In python, we use OS module<\/code> to interact with the operating system. As it comes under Python\u2019s standard utility module, so we do not have to install it externally. During working if we use invalid or inaccessible file names and paths, then all functions in OS module raise OS error<\/code>.<\/p>\n

So at the top of file we have to import os module<\/code>\u00a0to use this functions.<\/p>\n

While we deal with files in directories in python we use two types of paths i.e<\/p>\n

    \n
  1. Relative Path : Location begins from the current working directory.<\/li>\n
  2. Absolute Path : Location begins from the root directory.<\/li>\n<\/ol>\n

    Where, current working directory refers to the folder in which the Python script is operating.<\/p>\n

    So, in this article we will discuss how we can get and change the current working directory in python.<\/p>\n

    Get the Current Working Directory in Python :<\/h3>\n

    getcwd()<\/code> method of the os module is used to get the absolute path of current working directory.<\/p>\n

    Syntax : os<\/span>.<\/span>getcwd<\/span>()<\/span><\/code><\/pre>\n

    So, let’s see a program, to understand the implementation of it.<\/p>\n

    #Program :\r\n\r\nimport os\r\n  \r\n# Function to Get the current working directory\r\ndef current_path():\r\n    print(\"Current working directory before change\")\r\n    print(os.getcwd())\r\n    print()\r\n  \r\n# Printing CWD before change\r\ncurrent_path()<\/pre>\n
    Output :\r\nCurrent working directory before change\r\nhome\/linuxize\/desktop<\/pre>\n

    Change the Current Working Directory in Python :<\/h3>\n

    os.chdir( )<\/code> method is used to change the current working directory in python.<\/p>\n

    Syntax : os<\/span>.chdir<\/span>()<\/span><\/code><\/pre>\n

    So, let’s see a program, to understand the implementation of it.<\/p>\n

    #Program :\r\n\r\nimport os\r\n  \r\n# Function to Get the current working directory\r\ndef current_path():\r\n    print(\"Current working directory after change\")\r\n    print(os.getcwd())\r\n    print()\r\n  \r\n  \r\n# Changing the current working directory\r\nos.chdir('\/temp')\r\n# Printing current working directory after\r\ncurrent_path()<\/pre>\n
    Output :\r\nCurrent working directory after change\r\n\/temp<\/pre>\n

    Changing the path which does not exist :<\/h3>\n

    Suppose a path does not exist, but we are trying to change that path using os.chdir( ) method. As we are trying to change a false path which does not exist so it will raise an error FileNotFoundError<\/code>.<\/p>\n

    So, let’s see a program, to understand the implementation of it.<\/p>\n

    #Program\r\n# Python program to change the current working directory\r\n \r\n# importing necessary libraries \r\nimport sys, os \r\n    \r\n# initial directory \r\ncwd = os.getcwd() \r\n    \r\n# This directory does not exist\r\nfd = 'false_dir\/temp'\r\n    \r\n# But we are trying to insert to flase directory \r\ntry: \r\n    print(\"Inserting inside :\", os.getcwd())\r\n    os.chdir(fd) \r\n        \r\n# Cache the exception  \r\nexcept: \r\n    print(\"Some error associated with specified directory. Exception : \")\r\n    print(sys.exc_info()) \r\n              \r\n# handling with finally           \r\nfinally: \r\n    print()\r\n    print(\"Restoring the correct path\") \r\n    os.chdir(cwd) \r\n    print(\"So Current directory is :\", os.getcwd())<\/pre>\n
    Output :\r\nInserting inside : home\/linuxize\/desktop\r\nSome error associated with specified directory. Exception :\r\n(<class \u2018FileNotFoundError\u2019>, FileNotFoundError(2, \u2018No such file or directory\u2019), <traceback object at 0x7fca10a2e788>)\r\nRestoring the correct path\r\nSo Current directory is :\r\nhome\/linuxize\/desktop<\/pre>\n","protected":false},"excerpt":{"rendered":"

    How to get and change current working directory in python ? In python, we use OS module to interact with the operating system. As it comes under Python\u2019s standard utility module, so we do not have to install it externally. During working if we use invalid or inaccessible file names and paths, then all functions …<\/p>\n

    How to change current working directory in python ?<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nHow to change current working directory in python ? - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to change current working directory in python ? - Python Programs\" \/>\n<meta property=\"og:description\" content=\"How to get and change current working directory in python ? In python, we use OS module to interact with the operating system. As it comes under Python\u2019s standard utility module, so we do not have to install it externally. During working if we use invalid or inaccessible file names and paths, then all functions … How to change current working directory in python ? Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-21T02:09:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:15:03+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#webpage\",\"url\":\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/\",\"name\":\"How to change current working directory in python ? - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-04-21T02:09:12+00:00\",\"dateModified\":\"2021-11-22T13:15:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to change current working directory in python ?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"How to change current working directory in python ?\",\"datePublished\":\"2021-04-21T02:09:12+00:00\",\"dateModified\":\"2021-11-22T13:15:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#webpage\"},\"wordCount\":265,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to change current working directory in python ? - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to change current working directory in python ? - Python Programs","og_description":"How to get and change current working directory in python ? In python, we use OS module to interact with the operating system. As it comes under Python\u2019s standard utility module, so we do not have to install it externally. During working if we use invalid or inaccessible file names and paths, then all functions … How to change current working directory in python ? Read More »","og_url":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-04-21T02:09:12+00:00","article_modified_time":"2021-11-22T13:15:03+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#webpage","url":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/","name":"How to change current working directory in python ? - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-04-21T02:09:12+00:00","dateModified":"2021-11-22T13:15:03+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"How to change current working directory in python ?"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"How to change current working directory in python ?","datePublished":"2021-04-21T02:09:12+00:00","dateModified":"2021-11-22T13:15:03+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#webpage"},"wordCount":265,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/how-to-change-current-working-directory-in-python\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3089"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3089"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3089\/revisions"}],"predecessor-version":[{"id":3125,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3089\/revisions\/3125"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}