In this python tutorial, you will learn how to get current directory in python.
Get current directory Python
In python, to get the current directory in python we will use os module which has a method getcwd() which will return the current working directory with full path.
The current directory is the folder from where the script is running.
For getting the name of the directory we can use another function called basename from os.path.
import os directory_path = os.getcwd() print("My current directory is : " + directory_path) folder_name = os.path.basename(directory_path) print("My directory name is : " + folder_name)
After writing the above code (get current directory Python), Ones you will print “directory_path” then the output will appear as “My current directory is E:\project-python“. Here, we used getcwd() for getting the current working directory which is the full path, and basename is used for getting directory name.
You can refer to the below screenshot get current directory Python
You may like following Python tutorials:
- Syntaxerror return outside function python
- Remove character from string Python
- Create an empty array in Python
- Invalid syntax in python
- syntaxerror invalid character in identifier python3
- How to handle indexerror: string index out of range in Python
- Unexpected EOF while parsing Python
- Python invalid literal for int() with base 10
- String methods in Python with examples
- Python create empty set
- Function in Python
- Python Read CSV File and Write CSV File
In this tutorial, we learned how to get current directory and the name of the directory in Python.