Python Practice › OS Module Programs › Program 39
Rename a folder
Write a Python program to rename a folder with os.rename().
Output
False True
Approach
os.rename(old, new).
Solution
import os os.makedirs("old_name", exist_ok=True)if os.path.exists("new_name"): os.rmdir("new_name")os.rename("old_name", "new_name")print(os.path.isdir("old_name"), os.path.isdir("new_name"))Output
False True
Learn the concept
New to this topic? Read the lesson, then come back.