Get Directory of the current python script

aasik

Aasik Muththalif

Posted on February 16, 2020

Get Directory of the current python script

using Python os module, we can get the current directory of python scripts and change the directory. The os module is included in the standard library, so no additional installation is required. This module provides a portable way of using operating system dependent functionality.

Here the following contents will be described.

Get the current working Directory
os.getcwd() returns the absolute path of the working directory where python is currently running as a string.

import os
currentDirectory = os.getcwd()
print("Current Working Directory")
print(currentDirectory)

Change the current working directory
We use chdir() to change the directory.

import os
os.chdir("D/python")
print("Directory Changed")
print(os.getcwd())

I hope this will help you to understand about directories and python.

💖 💪 🙅 🚩
aasik
Aasik Muththalif

Posted on February 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related