
- 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 directory if it does not exist using Python?
Python has built in file creation, writing, and reading capabilities. In Python, there are two sorts of files that can be handled: text files and binary files (written in binary language, 0s, and 1s). While you can create files you may delete them when you no longer need them.
It is simple to create directories programmatically, but you must ensure that they do not already exist. You'll have difficulties if you don't.
Example 1
In Python, use the os.path.exists() method to see if a directory already exists, and then use the os.makedirs() method to create it.
The built in Python method os.path.exists() is used to determine whether or not the supplied path exists. The os.path.exists() method produces a boolean value that is either True or False depending on whether or not the route exists.
Python's OS module includes functions for creating and removing directories (folders), retrieving their contents, altering and identifying the current directory, and more. To interface with the underlying operating system, you must first import the os module.
#python program to check if a directory exists import os path = "directory" # Check whether the specified path exists or not isExist = os.path.exists(path) #printing if the path exists or not print(isExist)
Output
On executing the above program, the following output is generated.
True Let’s look at a scenario where the directory doesn’t exist.
Example 2
The built in Python method os.makedirs() is used to recursively build a directory.
#python program to check if a directory exists import os path = "pythonprog" # Check whether the specified path exists or not isExist = os.path.exists(path) if not isExist: # Create a new directory because it does not exist os.makedirs(path) print("The new directory is created!")
Output
On executing the above program, the following output is generated.
The new directory is created!
Example 3
To create a directory, first check if it already exists using os.path.exists(directory). Then you can create it using −
#python program to check if a path exists #if it doesn’t exist we create one import os if not os.path.exists('my_folder'): os.makedirs('my_folder')
Example 4
The pathlib module contains classes that represent filesystem paths and provide semantics for various operating systems. Pure paths, which give purely computational operations without I/O, and concrete paths, which inherit from pure pathways but additionally provide I/O operations, are the two types of path classes.
# python program to check if a path exists #if path doesn’t exist we create a new path from pathlib import Path #creating a new directory called pythondirectory Path("/my/pythondirectory").mkdir(parents=True, exist_ok=True)
Example 5
# python program to check if a path exists #if path doesn’t exist we create a new path import os try: os.makedirs("pythondirectory") except FileExistsError: # directory already exists pass
- Related Articles
- How can I create a python directory if it does not exist?
- 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?
- How can you avoid getting an error if you are deleting a table which does not exist using Python?
- How can I create directory tree using C++ in Linux?
- Insert records in MongoDB collection if it does not exist?
- How can I check if some text exist or not in the page using Selenium?
- How to create a directory using Python?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- Upsert in MongoDB while using custom _id values to insert a document if it does not exist?
- How to create a directory recursively using Python?
- How to create a unique directory name using Python?
