Python Practice › OS Module Programs › Program 30
Change the working directory
Write a Python program to change the current working directory.
Output
workspace True
Approach
os.chdir(); remember the old folder to go back.
Solution
import os start = os.getcwd()os.makedirs("workspace", exist_ok=True)os.chdir("workspace")print(os.path.basename(os.getcwd()))os.chdir(start)print(os.getcwd() == start)Output
workspace True
Learn the concept
New to this topic? Read the lesson, then come back.