Get the current working directory

EasyOS Module Programs · 1 of 49

Write a Python program to get the current working directory.

Advertisement
Output
1
True

Approach

  1. os.getcwd() returns the folder the program runs in.

Solution

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

More os module programs

← All 49 OS Module Programs

Leave a Comment