Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

	
# Directory
directory = "ranjeet"

# Parent Directory path
parent_dir = "/home/User/Documents/Softhunt"

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

# Create the directory
# 'ranjeet'
os.makedirs(path)
print("Directory '%s' created" %directory)
Comment

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
Comment

PREVIOUS NEXT
Code Example
Python :: Creating a list with several elements that are distinct or duplicate 
Python :: List Comprehension simple example 
Python :: matplotlib legend from scratch 
Python :: how to change multiple index in list in python 
Python :: display csv data into flask api 
Python :: for i in range(6, 11): print(i, end="") 
Python :: merge sort dictionary python 
Python :: Add 1 to loops 
Python :: prolog split list positive negative 
Python :: Python slides 
Python :: python create empty list with size 10 
Python :: python certificate verify failed unable to get local issuer certificate nltk 
Python :: Annotation graphique python 
Python :: Explaining async session in requests-html 
Python :: Python NumPy atleast_1d Function Example when inputs are in high dimension 
Python :: fuck you 
Python :: differences between Pool.apply, Pool.apply_async, Pool.map and Pool.map_async. 
Python :: Python NumPy concatenate Function Example when axis equal to 0 
Python :: tensorflow configure multiple gpu 
Python :: assignment 8.4 python data structures 
Python :: Python how to use __le__ 
Python :: calculate mse loss python 
Python :: NumPy right_shift Code When inputs and bit shift are an arrays 
Python :: qt list widget let editable 
Python :: python decouple default value 
Python :: python call c function 
Python :: lda from scratch implementation on iris python 
Python :: pixel accuracy image segmentation python 
Python :: how do i access individual elements of matrix in python? 
Python :: Flask - how do I combine Flask-WTF and Flask-SQLAlchemy to edit db models 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =