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 an os module; to get this, you need to import the os module to the program. That is done as:
import os
Now let us see how to create a new directory in Python. To create a new directory, Python provides the mkdir() method. This function also throws the error FileExistsError when the directory already exists and creating the same again.
Syntax:
mkdir(path)
Path: You need to provide the address or path to where to create the directory.
Example #1
Code:
import os
os.mkdir("/home/Educba articles Directory")
print("Directory is created")
Output:
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:
Example #2
Code:
import os
dir = "Educba articles Directory"
try:
os.mkdir(dir)
print ("Directory is created")
except FileExistsError:
print ("Directory already exists")
Output:
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.
Example #3
Code:
import os
dirpath = "/home/Educba articles"
os.mkdir(dirpath)
print ("Directory is created")
Output:
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 the 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 the os module.
Code:
import os
curworkdir =os. getcwd()
print ("The current working directory is", curworkdir)
Output:
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.
Syntax:
chdir(path_location)
Code:
import os
os.chdir('E : \ \Educba article')
curworkdir = os.getcwd()
print (curworkdir)
3. Renaming Directory
Directories can also be renamed after the creation of directories. To do this, we can use the function os. rename(), and we have to pass the correct path of the director, which has to be renamed.
Syntax:
rename(old_dir_name, new_dir_name)
Code:
import os
olddir = os.path.join("E:\ \Educba article\ Python")
newdir = os.path.join ("E:\ \Educba article\Python2")
if os.path.exists(olddir):
os.rename(olddir, newdir)
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.
Syntax:
rmdir(dir_name)
Code:
import os
dirrm = os.path.join ("E:\ \Educba article\Python2")
if os.path.exists(dirrm)
os.rmdir(dirrm)
print(dirrm + "Directory has been deleted")
Output:
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.
Syntax:
listdir(specific_location)
Code:
import os
print ("The lists of directories in the above-mentioned location are:")
print(os.listdir("/home"))
Output:
Conclusion
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 to directories, such as creating, getting the 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.
Recommended Articles
We hope that this EDUCBA information on “Python Create Directory” was beneficial to you. You can view EDUCBA’s recommended articles for more information.