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 existsimport ospath = "directory"# Check whether the specified path exists or notisExist = os.path.exists(path)#printing if the path exists or notprint(isExist)
Output
On executing the above program, the following output is generated.
TrueLet’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 existsimport ospath = "pythonprog"# Check whether the specified path exists or notisExist = 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 oneimport osif 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 pathfrom pathlib import Path#creating a new directory called pythondirectoryPath("/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 pathimport ostry: os.makedirs("pythondirectory")except FileExistsError: # directory already exists pass
Updated on: 06-Jun-2024
278K+ Views
- Related Articles
- MySQL create user if it does not exist?
- How to create a folder if it does not exist in C#?
- 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?
- 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 can I create directory tree using C++ in Linux?
- 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 using Python?
- How to Create a Sheet if Not Exist in the Workbook
- How to create a directory recursively using Python?
- MongoDB query to determine if a specific value does not exist?
- How to create a unique directory name using Python?
- Golang Program to check a directory is exist or not
Advertisem*nts