Python Practice › OS Module Programs › Program 1
Get the current working directory
Write a Python program to get the current working directory.
Output
1 True
Approach
os.getcwd()returns the folder the program runs in.
Solution
import os cwd = os.getcwd()print(os.path.basename(cwd))print(os.path.isabs(cwd))Output
1 True
Line by line
Line 3
The full path depends on your computer, so the example prints only the last folder name.
Line 5
getcwd() always returns an absolute path.
Learn the concept
New to this topic? Read the lesson, then come back.