How to Obtain the Current Working Directory of a Program in Python 


               


How to Obtain the Current Working Directory of a Program in Python



Python


In this article, we show how to obtain the current working directory of a program in Python.

All programs (files, software) runs in certain directories.

If you are running a Python program, such as on your operating system on the Python IDLE, it is running on a certain directory on your computer.

Python has built-in functions that can allow you to obtain this current working directory, so you can see the full pathway where the program is running.

The line of code to get the current working directory in Python is, os.getcwd().

The full code to get the current working directory is shown below.



First, we must import the os module.

After this, get the current directory through the line, os.getcwd()

After running the code shown above, I got the following output from my computer shown below.



So the path you see above is the current working directory.

This is useful because you can see where you're currently executing a program; so, if you need to change directories, you can do so.


Related Resources



HTML Comment Box is loading comments...