- How to get the current directory in Python?
- Table of Contents
- What are directories and how do they work?
- Python get current directory:
- Syntax of os.getcwd:
- Code for python get current directory:
- Python change directory
- Syntax of chdir():
- Parameters:
- Code to change current directory:
- Limitations and Caveats:
- Python Get Current Directory – Print Working Directory PWD Equivalent
- How to Get The Current Directory Using the os.getcwd() Method in Python
- How to Get The Current Directory Using the Path.cwd() Method in Python
- Conclusion
- How to get current directory name in Python
- Python os module
- Get Current Directory in Python Using os.getcwd()
- Get Directory Name Using os.path.dirname()
- get the current directory name in Python using pathlib
- Conclusion
How to get the current directory in Python?
In this short tutorial, let us look at how you could use python to get the current directory and how to change the working directory.
In case you are here only for the solution of getting the current directory in Python, use this link.
Table of Contents
What are directories and how do they work?
In case you are new to programming, directories are nothing but folders. These directories are present inside a root folder eg: C:\ or D:\ and each directory could contain files or subdirectories.
To retrieve a file in Python, you need to know the exact path to reach the file, in Windows, you can view a particular file’s path by right-clicking the File-> Properties-> General-> Location.
Similarly, to run a script, the working directory needs to be set to the directory containing the script. However, while trying to run multiple scripts or while handling files the Current Working Directory (CWD) is important.
Python would not be able to access the files if they aren’t in the CWD. It is in these scenarios that the Python ‘get current directory’ command helps you know which directory you are in currently.
Python get current directory:
To return the directory you are currently in, we use the OS module to interact with the operating system. Under the OS module, we use the os.getcwd() method to return the path of the current directory.
Syntax of os.getcwd:
Code for python get current directory:
#importing the os module import os #to get the current working directory directory = os.getcwd() print(directory)
The output may vary depending on the directory you are in but it would start from the root folder eg: D:\ and the directory prefixed by a \ .
Python change directory
Similar to the os.getcwd method we used in Python to get the current directory, we use the chdir() methods in the os module to change the current directory.
The current directory is changed to retrieve files or run scripts that are present in other directories.
Syntax of chdir():
Parameters:
Code to change current directory:
Let’s say I want to change the current directory to a subdirectory called «freelancer» that is present inside the «flexiple» directory.
import os os.chdir("C:\flexiple\freelancer")
Limitations and Caveats:
- The os.getcwd method only returns the current working directory, in case you want the entire path, use the os.path.realpath(file) method
- Unlike the os.getcwd the change directory requires a parameter that needs to be a directory, if not, Python returns a NotADirectoryError
- If the directory does not exist then a FileNotFoundError is returned. And in case the user lacks the necessary permissions to access the directory a 1PermissionError is returned
Python Get Current Directory – Print Working Directory PWD Equivalent
Dionysia Lemonaki
In this article, you will learn how to get the current working directory (another name for folder) in Python, which is the equivalent of using the pwd command.
There are a couple of ways to get the current working directory in Python:
- By using the os module and the os.getcwd() method.
- By using the pathlib module and the Path.cwd() method.
How to Get The Current Directory Using the os.getcwd() Method in Python
The os module, which is part of the standard Python library (also known as stdlib), allows you to access and interact with your operating system.
To use the os module in your project, you need to include the following line at the top of your Python file:
Once you have imported the os module, you have access to the os.getcwd() method, which allows you to get the full path of the current working directory.
Let’s look at the following example:
import os # get the current working directory current_working_directory = os.getcwd() # print output to the console print(current_working_directory) # output will look something similar to this on a macOS system # /Users/dionysialemonaki/Documents/my-projects/python-project
The output is a string that contains the absolute path to the current working directory – in this case, python-project .
To check the data type of the output, use the type() function like so:
print(type(current_working_directory)) # output #
Note that the current working directory doesn’t have a trailing forward slash, / .
Keep in mind also that output will vary depending on the directory you are running the Python script from as well as your Operating System.
How to Get The Current Directory Using the Path.cwd() Method in Python
In the previous section, you saw how to use the os module to get the current working directory. However, you can use the pathlib module to achieve the same result.
The pathlib module was introduced in the standard library in Python’s 3.4 version and offers an object-oriented way to work with filesystem paths and handle files.
To use the pathlib module, you first need to import it at the top of your Python file:
Once you have imported the pathlib module, you can use the Path.cwd() class method, which allows you to get the current working directory.
Let’s look at the following example:
from pathlib import Path # get the current working directory current_working_directory = Path.cwd() # print output to the console print(current_working_directory) # output will look something similar to this on a macOS system # /Users/dionysialemonaki/Documents/my-projects/python-project
As you can see, the output is the same as the output I got when I used the os.getcwd() method. The only difference is the data type of the output:
print(type(current_working_directory)) # output #
Conclusion
And there you have it! You now know how to get the full path to the current directory in Python using both the os and pathlib modules.
Thanks for reading, and happy coding!
How to get current directory name in Python
In this Python tutorial, I will explain to you, how to get current directory name in Python. By using the Python os module, we will see, how to get current folder name in Python.
Python os module
Before we start, it is essential to understand the ‘os’ module. Python’s os module provides functions to interact with the operating system. This module comes in handy when we want to work with files and directories. One of the common tasks we can do with this module is to get the current directory name.
Get Current Directory in Python Using os.getcwd()
The getcwd() function of the os module returns the current working directory in Python. Here’s how we can use it to get the current folder in Python.
import os current_directory = os.getcwd() print(current_directory)
When you run this code, it will print the full path of your current working directory.
To get just the name of the current directory (and not the full path) or the folder name, we can use the os.path.basename() function. Here’s how we can use it:
import os current_directory = os.getcwd() directory_name = os.path.basename(current_directory) print(directory_name)
This code will print the name of the current directory in Python, without the full path. Or this is how to get the folder name in Python.
Get Directory Name Using os.path.dirname()
Another function from the os module that we can use to get the directory name is os.path.dirname() . The dirname() function returns the directory component of a pathname. However, in most cases, if we want to get the current directory, we still have to use it in conjunction with os.getcwd() :
import os current_directory = os.getcwd() print(os.path.dirname(current_directory))
This code will print the directory that contains the current directory, not the current directory itself.
To get just the name of the current directory (and not the full path), we can use the os.path.basename() function in conjunction with os.path.dirname() :
import os current_directory = os.getcwd() directory_name = os.path.basename(os.path.dirname(current_directory)) print(directory_name)
get the current directory name in Python using pathlib
Pathlib is a module in Python used for object-oriented filesystem paths. It was designed to be simple to use and to represent filesystem paths with semantics appropriate for different operating systems. Here’s how to get the current directory using pathlib in Python:
from pathlib import Path # Get current directory current_directory = Path.cwd() print(current_directory)
Just like with the os module, this will print the full path of the current directory. To get just the name of the current directory, we can use the .name attribute:
from pathlib import Path # Get current directory current_directory = Path.cwd() # Get current directory name directory_name = current_directory.name print(directory_name)
Conclusion
In this Python tutorial, We’ve looked at different methods to get the current directory in Python, using both the os and pathlib modules.
Remember, os.getcwd() and Path.cwd() give you the current working directory as a full path in Python. If you want only the name of the current directory, you can use os.path.basename(os.getcwd()) or Path.cwd().name .
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.