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 :: add Elements to Python list Using extend() method 
Python :: Python return statement (Write and Call Function) 
Python :: Grading program using if else 
Python :: foreach on sysargv 
Python :: split x and y pandas 
Python :: numpy find most distant elements in array 
Python :: glom safely interact with dictionary 
Python :: for loop for many integers in list 
Python :: basic kivy md 
Python :: word search engine in python 
Python :: python create empty list with size 
Python :: OSError Traceback (most recent call last) <ipython-input-74-8920269c5588 in <module() 9 10 run_with_ngrok(app) --- 11 app.run() 
Python :: python typing optional argument 
Python :: Javascript rendering problem in requests-html 
Python :: Python NumPy transpose Function Example in one line of code 
Python :: geopandas nc file 
Python :: python generate string of length 
Python :: Python NumPy concatenate Function Example when axis equal to none 
Python :: vocal remover source code python 
Python :: midpoint line drawing algorithm 
Python :: Python __le__ magic method 
Python :: simpy 
Python :: NumPy packbits Syntax 
Python :: center pyfiglet to terminal 
Python :: how to show all rows whith a unique value in a column 
Python :: pandas impute zero 
Python :: pygame getting your charecter to jump 
Python :: Examples of incorrect code for this rule: 
Python :: find sum of all elements in a matrix by python 
Python :: Redirecting an old URL to a new one with Flask micro-framework 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =