Introduction to Python Create Directory
In general, the directory is a folder or a container that has a set of files and other directories in it known as subdirectories. Similar to other programming languages in Python also the directories are meant to store files or subdirectories which in turn provides os module which comes under Python’s standard utility modules with a variety of methods that are used for the creation of directories. In Python, to create directories Python os module provides a function called mkdir() or makedirs(). In the os module all functions if not correctly defined then it raises an error which is caused when there are no proper file names and paths or arguments are written in these functions.
How to Create a Directory in Python with Examples
This article is based on how to handle directories in Python. Usually for reading and writing to the file does not require any kind of modules but for creation or handling of directories requires os module to get this you need to import os module to the program. That is done as:
Now let us see how to create a new directory in Python. To create a new directory Python provides mkdir() method. This function also throws error FileExistsError when the directory already exists and creating the same again
Path: you need to provide the address or path to where to create the directory.
os.mkdir("/home/Educba articles Directory")
print("Directory is created")
The above example creates the new directory in the current working directory and is named as “Educba articles Directory”. Suppose the above directory already exists then it will throw an error FileExistsError so to catch these errors we can use try and except blocks. This can be done as follows:
dir = "Educba articles Directory"
print ("Directory is created")
print ("Directory already exists")
In the above program the directory “Educba articles Directory” has already been created before the program, so it will be written Directory already exists instead of throwing error FileExistsError.
Now let us see how to create a directory with specifying the path which means where the directory must be created.
dirpath = "/home/Educba articles"
print ("Directory is created")
If there does not exist any such path it gives the following output:
The above example created the directory in the folder “Python” and this folder is in directory “Educba article” which is the current working directory. But if we want to create the above directory in the current working directory and if there is no “Python” folder in the directory “Educba article” then it will throw an error FileNotFoundError.
Functions of Python Create Directory
There are some functions in the os module that can be applied to the directories and can fetch different details related to directories that are created. Let’s see which are the and it is working:
1. Current working directory
The current working directory is the directory where the programs are saved in it. Suppose if you want to know what is the current working directory of any Python program then we can do this by using the getcwd() function of os module.
curworkdir =os. getcwd()
print ("The current working directory is", curworkdir)
2. Change the current working directory
This is another function of the os module which used when we want to change the current working directory to others. This can be done using the chdir() function.
os.chdir('E : \ \Educba article')
curworkdir = os.getcwd()
3. Renaming Directory
Directories can be renamed also after the creation of directories. To do this we can use function os. rename() and we have to pass the correct path of the director which has to be renamed.
olddir = os.path.join("E:\ \Educba article\ Python")
newdir = os.path.join ("E:\ \Educba article\Python2")
print("The directory is renamed", format(olddir, newdir)
4. Deleting the directory
As we can create the directories we can also delete the directories with the same process but we have a different function for this rmdir() to this we need to pass the path to which directory we want to delete or remove.
dirrm = os.path.join ("E:\ \Educba article\Python2")
print(dirrm + "Directory has been deleted")
Python2 Directory has been deleted.
5. Listing of Directories
Suppose if we want some list of directories for some specific location then we can get the list of directories for that location using the function listdir(). In this we need to pass the location then it will return us the list of directories in that location.
print ("The lists of directories in the above-mentioned location are:")
In this article, we saw how to create a directory in Python. Directories are the kind of folders in which we can store files or even other directories. In Python, creating directories is simple it has many different functions that can be applied on directories such as creating, getting current working directory, getting the lists of directories in the directory, renaming directories, deleting directories, etc. These all have separate built-in functions such as mkdir(), getcwd(), listdir(), rename(olddir, newdir), rmdir(), etc all are under os module which needs to be imported before using all these functions.
This is a guide to Python Create Directory. Here we discuss How to Create a Directory in Python with Examples and Functions. You may also have a look at the following articles to learn more –