One of the most powerful features of Python is its ability to provide you with information about the current Directory. This makes it easy to know where everything is in your program at any given time.
To get this information, just type the following command: Python -c “import os; print(os.getcwd())”
Directory
A directory includes a collection of different subdirectories and files that contain content. You can work with the modules mentioned below to work with directories.
Current Working Directory
The current working Directory is a special location in computer files. It contains the path to your Python script. You can use the Python directory() function to get the current working directory of your Python scripts.
Absolute and Relative Paths
An absolute path is one which provides you with the location of a file concerning the root directory.
On the other hand, the relative path takes into account the current Directory. There is no need to specify the root directory location here.
While specifying the relative path, a single dot (.) means the current Directory, while a double dot(..) means parent directory.
Getting Directory in Shell
The process of getting the current working Directory is as follows:
1) Open a new file or create a blank one.
2) Type in your Python code and save it with the .py extension.
3) Run the following command from a command prompt: cwd()
Getting a Directory in Python Program
Sometimes, you might want to check the current Directory in your Python program or get the current working directory for a project or script. Here are some ways to do that. os.getcwd() and os.chdir() are widely known functions to achieve our result.
Using os Module to get the current Directory
Various methods in the os module will help you get the desired results.
1. Using os.getcwd()
getcwd means get a current working directory and it is a part of the os module. Hence, you need to import this module before using the function. This function will provide you with the path of the file. The absolute path is the output in string format.
import os
path = os.getcwd()
print(path)
2. Using os.chdir()
If you wish to change the Directory, opt for this function. This function has a path as its argument. Hence, you need to specify the new path to transfer your file contents. chdir means change directory.
os.chdir('../')
print(os.getcwd())
3. Using os.path
Here, you can get the directory name from the provided path. You need to use the file path as an argument here. Using the file path, the function will provide a directory name.
dir_name = os.path.dirname(filepath)
print(dir_name)
4. Get a full path to the Directory
In this method, you can easily remove or get rid of symbolic links ( any shortcuts to directory location.) Output is of string data type.
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
Using Path Module to get Current Working Directory
The Path module is the newest addition to the list of methods used to get the current working directory in Python. It will easily provide you with the path of the Directory you’re currently working in.
from pathlib import Path
print(Path.cwd())
Working with Mac/Windows
Mac uses forward slash, while Windows uses backslash while specifying paths of directories. With the pathlib functions, go for forward slashes. It will convert into the right format on its own.
Using .absolute() with pathlib
With this function, you will obtain the absolute path of cwd.
import pathlib
print(pathlib.Path().absolute())
#this will give the current working directory's absolute path
Using .resolve() with pathlib
Here, you just have to import pathlib and use the resolve() function. Through this,you will obtain the current directory location in Python.
pathlib.Path().resolve()
#make sure that you import pathlib at the #beginning of function call
Get the current Directory and join
Supposedly you have a file in the current working directory, and you need to add it to a certain folder, you can use the join() method. For this, get the location of the current Directory using getcwd() and post this, join the current directory path to the desired destination folder path. Observe this example:
import os
current = os.getcwd()
answer = os.path.join(cwd, "destination/folder")
Python glob get current Directory
glob is a Python module that provides us with a list of files that are situated in a directory. glob is used to match patterns too. Its syntax is as follows:
My_path = os.getcwd()+'\\My_dir\\*'
for file_path in glob.glob(My_path):
print(file_path)
#this gives output as files with the related paths
Python get current Directory in Jupyter
Opt for pwd to check the current working directory in a Jupyter Notebook. os.getcwd() will also fetch the same result using the os module.
If you want to change the current Directory, enter:
os.chdir(' file_path ')
and verify using os.getcwd() again. You can check whether the Directory’s location has been changed or not.
Get current venv path
In case you wish to obtain the path of the virtual environment, use the VIRTUAL_ENV. You can apply this only if you have activated the entire setup. Import the os module first and then
print(os.environ['VIRTUAL_ENV'])
FAQs
It provides us with the absolute path of the file.
In Windows, we use backslash, but in Mac, we tend to use forward slash for directory traversal.
It has several methods like copy(), copy2(), etc, which aid in file traversal.
Trending: Python Check if File Exists – All Methods Covered
(Opens in a new browser tab)
Conclusion
Python is an amazing language for learning how to program. It has a lot of great features, but one of the most useful is its ability to set the current working Directory. This is especially useful if you want to work on more than one file at a time. In this article, we covered the ways to get the current working directory.