Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Handling errors while using os.makedirs() method

# Python program to explain os.makedirs() method

# importing os module
import os

# os.makedirs() method will raise
# an OSError if the directory
# to be created already exists
# But It can be suppressed by
# setting the value of a parameter
# exist_ok as True
	
# Directory
directory = "ranjeet"

# Parent Directory path
parent_dir = "/home/ranjeet/Desktop/Softhunt"

# Path
path = os.path.join(parent_dir, directory)

# Create the directory
# 'ranjeet'
try:
	os.makedirs(path, exist_ok = True)
	print("Directory '%s' created successfully" %directory)
except OSError as error:
	print("Directory '%s' can not be created")


# By setting exist_ok as True
# error caused due already
# existing directory can be suppressed
# but other OSError may be raised
# due to other error like
# invalid path name
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Handling #errors #method
ADD COMMENT
Topic
Name
9+1 =