
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can I create a python directory if it does not exist?
When programming in python, using Idiomatic python is normally the way to go. One of python idioms is EAFP: Easier to ask for forgiveness than permission. So try creating a directory, if it exists you'll get an error you can catch.
example
import os, errno try: os.makedirs('my_folder') except OSError as e: # If error is not already exists, then raise the error else continue if e.errno != errno.EEXIST: raise # Now do what you want with my_folder
- Related Articles
- How can I create a directory if it does not exist using Python?
- Copy and Create Destination Directory if it Does Not Exist on Linux
- MySQL create user if it does not exist?
- How to create a folder if it does not exist in C#?
- Create view in MySQL only if it does not already exist?
- How to check if a table exists in MySQL and create if it does not already exist?
- Insert records in MongoDB collection if it does not exist?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- How can you avoid getting an error if you are deleting a table which does not exist using Python?
- How can I check if some text exist or not in the page using Selenium?
- How can I create directory tree using C++ in Linux?
- MongoDB query to determine if a specific value does not exist?
- Golang Program to check a directory is exist or not
- How can I list the contents of a directory in Python?
- Upsert in MongoDB while using custom _id values to insert a document if it does not exist?

Advertisements