
- 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 to create a directory using Python?
To create a directory, first check if it already exists using os.path.exists(directory). Then you can create it using:
import os if not os.path.exists('my_folder'): os.makedirs('my_folder')
You can also use the python idiom EAFP: Easier to ask for forgiveness than permission. For example,
import os try: os.makedirs('my_folder') except OSError as e: if e.errno != errno.EEXIST: raise
- Related Articles
- How to create a directory recursively using Python?
- How to create a unique directory name using Python?
- How to create a zip archive of a directory using Python?
- How to create a directory using Java?
- How to create a Directory using C#?
- How to create a Directory recursively using Java?
- How to create a directory hierarchy using Java?
- How to remove a directory using Python?
- How to remove a directory recursively using Python?
- How to calculate a directory size using Python?
- How to create a directory in project folder using Java?
- How to rename directory using Python?
- How can I create a directory if it does not exist using Python?
- How to create a new directory in Linux using the terminal?
- How to change current directory using Python?

Advertisements