Make a directory safely without raising error
Use “exist_ok” parameter of makedirs method of os library to make a directory without an error.
# Imports
import os
# Let's define the path where we want a new directory
path = "D:\\New folder\\newdir"
# exist_ok = True doesn't create folder if it already exists and doesn't raise an error
os.makedirs(path, exist_ok=True)
That's how we Make a directory safely without raising error
That’s all for this mini tutorial. To sum it up, we learned how to Make a directory safely without raising error.
Hope it was easy, cool and simple to follow. Now it’s on you.
Related Resources:
- Check if file or directory exists in Python Check if file or directory exists Use “path.exists” method of os library to check if file or directory exists for...
- Create a list of files in a folder python Create a list of files in a folder python Create a list of files in a folder python | List all...
- Learn Python easily with Detailed tutorials Python Learn python easily and efficiently with mini tutorials one snippet at a time. Python is widely used language for...
- Yield Keyword Python Yield Keyword in Python Yield keyword when used inside a function definition returns a generator. Generator objects generate one value...
- Convert unix timestamp string to readable date in Python Unix Timestamp Conversion Converting unix timestamp string to readable date in python Converting unix timestamp string to readable date in...
- Merge two dictionaries in Python Merge two dictionaries in Python Use “**- unpacking operator” to merge two dictionaries # Let's create two dictionaries a =...
- Key in a dictionary Python Check if a key exists in a dictionary Existence of a key can be checked by .get() method of a...
- Sort Dictionary by Value Sort Dictionary by Value “Key” parameter of sorted function can be used to sort by value. # Let's create a...
- String Cases | Learn to use lower and upper case String Cases String method lower() converts string to all lower case. String method upper() converts string to all upper case. ...
- Pandas series from Dictionary | Pandas Tutorial Pandas Series from Dictionary Create pandas series from dictionary using “Series” method of Pandas library. In the below example we...