In this tutorial, we will look at how to get the current working directory in Python.
There are a number of ways to get the current working directory. You can use the
os standard library’s
getcwd() function. Or, you can use the pathlib library’s
Path.cwd() class method. Let’s look at these methods with some examples.
1. Using the
os is a Python standard library, you don’t need to install it additionally. It provides a number of useful functions to interact with the operating system (for example, using functions like getcwd(), chdir(), etc to interact with the file system).
The simplest way to get the current directory in python is to use the
os.getcwd() function. It returns the absolute path of the current working directory as a string. The following is the syntax:
import os print(os.getcwd())
You can see that we get the absolute path of the current directory returned as a string.
Additionally, you can use the
os.chdir() function to change the current working directory, for example, in the above scenario, to change the current directory to the “Documents” folder, which is two folders above the current folder “Article”, use the following syntax:
import os # change the current working directory os.chdir('..\..') # print the current working directory print(os.getcwd())
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
You can see that now the current working directory has been changed to the “Documents” folders.
2. Using the
You can also use the
pathlib module in python to get the current working directory. The pathlib module has been available in the standard library since Python 3.4 and comes with a number of useful functions for file handling.
You can use the
Path.cwd() function to get the current working directory. The following is the syntax:
from pathlib import Path print(Path.cwd())
In the above output, you can see that we get the current working directory as output. Notice that it is the “Documents” folder. This is due to the
os.chdir() command that we performed in the previous example which changed our current directory to the “Documents” folder.
For more on the pathlib’s Path.cwd() method, refer to its documentation.
With this, we come to the end of this tutorial, The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on interacting with the file system in Python –