{"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":"
In python, we use So at the top of file we have to import While we deal with files in directories in python we use two types of paths i.e<\/p>\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 So, let’s see a program, to understand the implementation of it.<\/p>\n So, let’s see a program, to understand the implementation of it.<\/p>\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 So, let’s see a program, to understand the implementation of it.<\/p>\n 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>\nOS 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
os module<\/code>\u00a0to use this functions.<\/p>\n
\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
#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
#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
FileNotFoundError<\/code>.<\/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":"