DEV Community

Cover image for Get Directory of the current python script
Aasik Muththalif
Aasik Muththalif

Posted on

2 1

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.

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay