# How to change current working directory in python ?

## 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’s 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.

So at the top of file we have to import os module to use this functions.

While we deal with files in directories in python we use two types of paths i.e

1. Relative Path : Location begins from the current working directory.
2. Absolute Path : Location begins from the root directory.

Where, current working directory refers to the folder in which the Python script is operating.

So, in this article we will discuss how we can get and change the current working directory in python.

### Get the Current Working Directory in Python :

getcwd() method of the os module is used to get the absolute path of current working directory.

Syntax : os.getcwd()

So, let’s see a program, to understand the implementation of it.

#Program :

import os

# Function to Get the current working directory
def current_path():
print("Current working directory before change")
print(os.getcwd())
print()

# Printing CWD before change
current_path()
Output :
Current working directory before change
home/linuxize/desktop

### Change the Current Working Directory in Python :

os.chdir( ) method is used to change the current working directory in python.

Syntax : os.chdir()

So, let’s see a program, to understand the implementation of it.

#Program :

import os

# Function to Get the current working directory
def current_path():
print("Current working directory after change")
print(os.getcwd())
print()

# Changing the current working directory
os.chdir('/temp')
# Printing current working directory after
current_path()
Output :
Current working directory after change
/temp

### Changing the path which does not exist :

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.

So, let’s see a program, to understand the implementation of it.

#Program
# Python program to change the current working directory

# importing necessary libraries
import sys, os

# initial directory
cwd = os.getcwd()

# This directory does not exist
fd = 'false_dir/temp'

# But we are trying to insert to flase directory
try:
print("Inserting inside :", os.getcwd())
os.chdir(fd)

# Cache the exception
except:
print("Some error associated with specified directory. Exception : ")
print(sys.exc_info())

# handling with finally
finally:
print()
print("Restoring the correct path")
os.chdir(cwd)
print("So Current directory is :", os.getcwd())
Output :
Inserting inside : home/linuxize/desktop
Some error associated with specified directory. Exception :
(<class ‘FileNotFoundError’>, FileNotFoundError(2, ‘No such file or directory’), <traceback object at 0x7fca10a2e788>)
Restoring the correct path
So Current directory is :
home/linuxize/desktop