Welcome to the World of Online Learning:
Hello Friends “This blog helps you to learn Python programming concepts. You can learn Python language at your own speed and time. One can learn concepts of Python language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”
Write a Python Program to Safely Create a Nested Directory
PROGRAM: Python Program to Safely Create a Nested Directory
/* Python Program to Safely Create a Nested Directory */
1: Using pathlib.Path.mkdir
from pathlib import Path
Path("/root/dirA/dirB").mkdir(parents=True, exist_ok=True)
2: Using os.makedirs
import os
os.makedirs("/root/dirA/dirB")
3: Using distutils.dir_util
import distutils.dir_util
distutils.dir_util.mkpath("/root/dirA/dirB")
4: Raising an exception if directory already exists
import os
try:
os.makedirs("/dirA/dirB")
except FileExistsError:
print("File already exists")