- Get and change the current working directory in Python
- Get the current working directory: os.getcwd()
- Change the current working directory: os.chdir()
- 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
- Python get current working directory
- Get current working directory with os.getcwd
- Get current working directory with Path.cwd
- Get current working directory with os.path
- Author
Get and change the current working directory in Python
In Python, you can get and change (set) the current working directory with os.getcwd() and os.chdir() . The os module is part of the standard library, so no additional installation is necessary, but you need to import it.
You can get the path of the current script file ( .py ) with __file__ . See the following article.
Get the current working directory: os.getcwd()
os.getcwd() returns the absolute path of the current working directory, where Python is running, as a string ( str ).
getcwd stands for «get current working directory», and the Unix command pwd stands for «print working directory». Of course, you can print the current working directory with os.getcwd() and print() .
import os path = os.getcwd() print(path) # /Users/mbp/Documents/my-project/python-snippets/notebook print(type(path)) #
Use the os.path module to manipulate and handle path strings effectively. See the following article for details.
Change the current working directory: os.chdir()
You can change (set) the current working directory using os.chdir() .
Specify the target path as an argument, which can be either absolute or relative. Use ‘../’ to move up one directory level.
os.chdir() changes the current directory similar to the Unix command cd . Both chdir and cd stand for «change directory».
os.chdir('../') print(os.getcwd()) # /Users/mbp/Documents/my-project/python-snippets
With the __file__ and os.path functions, you can change the current directory to the directory containing the running script file ( .py ).
os.chdir(os.path.dirname(os.path.abspath(__file__)))
See the following article for details.
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!
Python get current working directory
Python get current working directory tutorial shows ways how to find out the current working directory in Python.
Current working directory is a full path wheare a program is executed.
$ pwd /janbodnar/Documents/prog/python/getcwd
We can find out the current working directory with the pwd command.
Get current working directory with os.getcwd
The os.getcwd returns a string representing the current working directory.
#!/usr/bin/python import os w_dir = os.getcwd() print(w_dir)
The program prints the current working directory with os.getcwd .
$ ./os_getcwd.py /janbodnar/Documents/prog/python/getcwd
Get current working directory with Path.cwd
The Path.cwd returns a new path object representing the current directory.
#!/usr/bin/python from pathlib import Path work_dir = Path.cwd() print(work_dir)
The program prints the current working directory with Path.cwd .
Get current working directory with os.path
The __file__ is a special Python build-in variable which contains the path to the currently running script. Since Python 3.9, the value is an absolute path. In earlier versions, the path could be relative.
When traversing file system hierarchy, we can use the . for current working directory, and the .. for parent directory.
#!/usr/bin/python import os print(os.path.dirname(os.path.normpath(__file__))) print(os.path.abspath('.'))
The os.path.dirname returns the directory name of the given path. The normpath method normalizes a pathname by collapsing redundant separators and up-level references. The path.abspath returns a normalized absolutized version of the given pathname.
In this tutorial we have shown ways of finding the current working directory in Python.
Author
My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.