Os.mkdir trong python
In many programming languages, the “mkdir” command is used to create a new directory. In Python, the OS module interacts with the operating system and provides different functions like creating and deleting file directories. The “mkdir” method is used to create the directory by declaring “os.mkdir()” in Python script with its “path name” and “numeric mode” value inside the argument. We can create temporary and permanent directories using a different module in Python code. Show This write-up will explain a detailed understanding of the Python create directory method “os.mkdir()”. The outcomes are as follows: So let’s get started! What is the os.mkdir() Method?The “os.mkdir()” method creates the new directory file in Python. This method is obtained from the os module which has various functions like creating or deleting a directory. The “os.mkdir()” method shows an error when the directory file already exists in the same location. How to Install Packages in Ubuntu Please enable JavaScript The syntax of “os.mkdir()” is shown below:
In the above syntax:
Let’s see how the os.mkdir() method works: Example 1: How to Create a Directory Using os.mkdir() Method?The “os.mkdir()” method creates a new directory by only specifying the path name inside the parentheses. The “mode” value and other parameters are optional to define inside the “os.mkdir()” method. Let’s understand the concept by following code: Code
In the above code:
Note: The “/” forward slash used before the file named “newfile” while defining the path is mandatory to use to create a new Directory. Output The above console output shows that a new directory is created. In the above snippet, the directory named “newfile” is created. Example 2: How to Handle Directory Already Exists Error Using os.mkdir() Method?Sometimes, when we create a new directory, an error like “file name already exists” or “file name error” occurs in the program. These errors are resolved by creating a new file with a different directory name or removing duplicates of the existing one. Let’s see an example below where this error is handled and removed using the “try-except” block. Code
In the above code:
Note: The “/” forward slash used before the file named “newfile” while defining the path is mandatory to use to create a new Directory. Output In the above output, a new directory is created by changing the name. The above snippet shows that the new directory named “newfile1” is created. That’s all from this Python tutorial. ConclusionIn Python, the “os.mkdir()” method is used to create new directories. The “os module” in Python provides functionality in which the python script can interact with the operating system and perform some operations, like creating a new directory or deleting the directory. The “already file exists error” can also be removed with the help of the “try-except” block and the “os.mkdir()” method. This tutorial has provided a detailed understanding of the “os.mkdir()” method with numerous examples. |