In this lesson we will learn about mkdirs method for Python.
Python mkdirs method
Python mkdirs method is a recursive function for creating directory. This method is just like mkdir method but the difference is that it makes all intermediate-level directories needed to contain the leaf directory. This method will raise an error exception if the leaf directory already exists or cannot be created. The default mode of this method is 0777
(octal). On some systems the mode is ignored.
Syntax
The syntax for Python mkdirs method is given below:
os.makedirs(path[, mode])
Parameters
There is only two parameters for makedirs method.
- path – This is the path which will be created recursively.
- mode – The Mode of the directories to be given (It is an optional parameter. The dafault value is 0777).
Example for Python mkdirs method
Here is an example for Python mkdirs method. You just define your path and call mkdirs function to create your directory(ies).
Here in my computer inside the home
directory there is a directory named admin
and I want to create a directory which will named mydir
inside the admin
directory. And I also want to give it a mode of 0755. Here is the example code for that:
#!/usr/bin/python import os, sys # The Path location you want to create path = "/home/admin/mydir" os.makedirs( path, 0755 );
When you run this script successfully you will see that inside home/admin
directory there is a new directory called mydir
.
The post Python mkdirs method: Python tutorial appeared first on JS Tricks.