loading...
Cover image for Get Directory of the current python script

Get Directory of the current python script

aasik profile image Aasik Muththalif ・1 min read

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.

Discussion

pic
Editor guide