Change the working directory

EasyOS Module Programs · 30 of 49

Write a Python program to change the current working directory.

Advertisement
Output
workspace
True

Approach

  1. os.chdir(); remember the old folder to go back.

Solution

Python
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.

More os module programs

← All 49 OS Module Programs

Leave a Comment