Get and change current working directory Python

Python PythonFile

In this post, we are going to learn how to Get and change the current working directory in Python and change the current working directory in Python with examples. The os module provides functions to handle the directory and files of the operating system. The os module is part of the standard library so no installation is required for this. We can directly import into our program.

While working with files and directories we have to use two-path


  • Absolute path: It represents the location/path of files or directories starting from the root directory.
  • RelativePath: It is the path of the current working directory, starting from the current working directory.It is the directory in which our code or script is runnning.To get the current working directory os.getcwd() method used.

1. Get current working directory


The os module os.getcwd() method is used to get the absolute path of the working directory and returns it as a string.

Steps to get working directory


  • The first step is to Import the os module using “import os”
  • Use the os.getcwd() method
  • Print Path using the print method
  • Find the location of script use print(os.path.realpath(__file__))

Python Program to get current working directory

import os

cwdpath = os.getcwd()

print('Current Path :\n',cwdpath)
print(type(cwdpath))
print('Script path :\n',os.path.realpath(__file__))

Output

Current Path :
 C:\Users\Admin\Desktop\Python_program
 <class 'str'>
Script path :
 C:\Users\nagen\Desktop\Python_program\sample.py

2. Change current working directory


To change the current working directory in python, the os.chdir() method is used. It takes an argument the path of the directory we want to change.

Syntax

os.chdir(path)

Parameter

path: It is the destination path. It can be an absolute or relative path to move up we use (. ./).

We use os.chdir(os.path.dirname(os.path.abspath(__file__))) to get path,where the script file is running.

Important notes

  • We have to make sure that given a path to chdir() method must a directory otherwise NotADirectoryError.
  • if the given directory does not exist at the given path it will raise the FileNotFoundError exception.So it is good practice to use try/catch block.
  • We can check directory exists at the given path by using if os.path.exists(“/Users/Admin/documents”):

Steps to change current directory

  • The first step is to Import the os module using “import os”.
  • Use the os.getcwd() method to get path of current working directory.
  • first checking if directory exist that we want to chnage.
  • Using os.chdir(‘/Users/Admin/documents’) to chnage current directory

Python Program to Change current working directory

import os

try:

 cwdpath = os.getcwd()
 print('Current Path :\n',cwdpath)

 if os.path.exists("/Users/Admin/documents"):

  os.chdir('/Users/Admin/documents')
  print('\n Path after change directory :\n', os.getcwd())

 else:
  print('directory does not exist :\n', os.getcwd())
 
except FileNotFoundError :
    print("directory does not is not Exist:") 
except NotADirectoryError:
    print("is not directory") 

Output

Current Path :
 C:\Users\Admin\Desktop\Python_program

Path after change directory ​
C:\Users\nagen\documents

Summary

In this post, we have learned how to Get and change the current working directory Python with examples.

  • os.getcwd() : to get the current directory
  • os.chdir() method to change the current directory