HomeLinuxPython OS Mkdir

Python OS Mkdir


There are a number of built-in Python modules for working with recordsdata and directories. The “os” module supplies a set of strategies that will let you work together with the working system, together with creating, deleting, and renaming directories. To arrange a brand new listing/folder, we are going to use the “os.mkdir()” technique. This can be a built-in technique offered by the “os” module in Python.

On this publish, we are going to discover the “Python os.mkdir()” technique utilizing quite a few examples. Let’s begin with the below-listed content material:

What’s the “Python os.mkdir()” Technique?

The “os.mkdir()” technique is utilized to create a brand new listing on the specified path. The tactic returns an error if the listing/folder already exists.

Syntax

os.mkdir(path, mode= 0o777, *, dir_fd= None)

Within the above syntax:

  • The “path” parameter specifies the trail of the listing the place it must be created.
  • The non-compulsory parameter “mode” signifies the mode of the listing to be created/generated. (Default worth is “0o777”).
  • dir_fd” serves as a file descriptor particularly for the listing. (Default worth is “None”).

Instance 1: Using the “os.mkdir()” Technique to Create a Single Listing
This instance is used to create a single listing at a specified location utilizing the “os.mkdir()” technique:

import os
os.mkdir(r‘C:UserspDocumentsprogramnew_directory’)
print(‘New Listing Efficiently Created!’)

Within the above code, apply the “os.mkdir()” to create a brand new listing by specifying the whole path together with the title of the listing, respectively, and print the said success message.

Output

Within the above output, it may be analyzed that the listing named “new_directory” has been created efficiently.

Instance 2: Using the “os.mkdir()” Technique to Create A number of Directories
On this instance, the mentioned technique will be utilized to create a number of directories as a substitute:

import os
location = r‘C:UserspDocumentsprogram’
directories = [‘directory1’, ‘directory2’, ‘directory3’]
for listing in directories:
    os.mkdir(os.path.be part of(location, listing))

Within the above code:

  • The trail of the listing is assigned to the variable “location”.
  • The title of the a number of directories is initialized in an inventory named “directories”.
  • Lastly, the “for” loop iterates over the checklist of directories and creates all of the a number of directories within the checklist utilizing the mixed “os.mkdir()” and “os.path.be part of()” strategies.

Output

The above output exhibits that a number of directories have been created efficiently.

Instance 3: Using the “os.mkdir()” Technique to Create Nested Directories
This instance applies the mentioned technique to create nested directories:

import os
os.mkdir(r‘C:UserspDocumentsprogramdirectory1new_directory’)

Within the above code block, the “os.mkdir()” technique creates a nested listing by specifying absolutely the path together with the “foremost” and the “nested” listing names, respectively.

Output

Within the above output, the nested listing “new_directory” has been created contained in the listing named “directory1” accordingly.

Instance 4: Using the “os.mkdir()” Technique to Deal with Errors
On this explicit instance, the mentioned technique can return an error if the listing already exists on the specified path. To deal with this error, the “try-except” block is utilized in this system by utilizing this instance code:

import os
attempt:
    os.mkdir(r‘C:UserspDocumentsprogramdirectory1′)
besides FileExistsError:
    print(“Listing Already Exists”)

Within the above code snippet:

  • Likewise, the listing is created utilizing the “os.mkdir()” technique within the “attempt” block.
  • The “besides” block code is used to deal with the possible error and show an exception message that signifies that the listing already exists on the specified path.

Output

The above snippet signifies that the listing i.e., “directory1” already exists on the specified path.

Conclusion

The “os.mkdir()” technique of the “os” module is used to create single, a number of, and nested directories in Python. This technique takes absolutely the path together with the listing title as an argument and creates a brand new listing. The “for” loop will be mixed with the “os.mkdir()” technique to create a number of directories and the “try-except” block to deal with the restrictions whereas making a listing. This publish introduced an in-depth information on the Python “os.mkdir()” technique with applicable examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments