Python Helpful Tutorials Tutorial
How To Get The Current Directory In Python
How To Get The Current Directory In Python
In this tutorial we will see how we can get the current directory in Python, or current working directory. When working with relative path in python, current directory plays an important part, because if we want to nevigate to any other folder we will use the current directory as reference, as starting point. Although we don't face such scenario when using absolute paths.
There are many ways to get the current directory, we will cover following ways:
- Get current working directory with os.getcwd
- Get current working directory with Path.cwd
- Get current working directory with os.path
Using os.getcwd
The getcwd() method of the os module in Python returns a string that contains the absolute path of the current working directory. The returned string does not include the trailing slash character.
Example:
# importing os module
import os
# Get the current working directory using getcwd
cwd = os.getcwd()
# Print the current working
print("Current working directory is:")
print(cwd)
Output:
Current working directory is: C:\PythonProjs\PathTest