Python path make dirs

How to Create a Directory in Python?

The os module is a built-in utility available in both Python 2 and 3 versions, and it provides functions to interact easily with the operating system. The os and os.path modules provide various functions to interact with the file system.

Let’s take a look at various ways through which you can create a directory in Python using the os module.

Method 1 – Using os.mkdir() function

The os.mkdir() method is used to create a directory in Python. This method will raise FileExistsError if the directory is already present in the specified path.

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

path: The location where you need to create a directory. The directory path will be a string object which includes the path and the directory name which needs to be created.

mode (optional): The permission that needs to be set to the newly-created directory. If you do not specify this parameter, then by default, it will set the permission as 0o777

dir_fd (optional): A file descriptor referring to a directory.

The default value will be set as None if you don’t provide any. If you specify the absolute path, then dir_fd is ignored.

Читайте также:  Java does class implement interface

Return Value – The os.mkdir() doesn’t return any value.

Example 1 – Create a directory in Python using os.mkdir()

The os.mkdir() creates the directory in the specified path if the directory not exists.

# Python program to create directory using os.mkdir() method import os # Directory path dir_path = "C:/Projects/Tryouts/sample" os.mkdir(dir_path) print("Directory '% s' created" % dir_path) # Directory path dir_path2 = "C:/Projects/Tryouts/sample2" # mode mode = 0o666 os.mkdir(dir_path2, mode) print("Directory '% s' created" % dir_path2) 
Directory 'C:/Projects/Tryouts/sample' created Directory 'C:/Projects/Tryouts/sample2' created

Example 2 – Exception if the directory already exists

The os.mkdir() method would raise a FileExistsError Exception if the directory in the location specified already exists.

# Python program to create directory using os.mkdir() method import os # Directory path dir_path = "C:/Projects/Tryouts/sample" os.mkdir(dir_path) print("Directory '% s' created" % dir_path) 
Traceback (most recent call last): File "c:\Projects\Tryouts\main.py", line 7, in os.mkdir(dir_path) FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Projects/Tryouts/sample'

Method 2 – Using os.makedirs() method to create a nested directory in Python

The os.makedirs() method is used to create a directory recursively in Python, which means while making the leaf directory, if there are any intermediate directory is missed, the method os.makedirs() will create all of them.

Syntax: os.makedirs(path, mode = 0o777, exist_ok = False)

path: The location where you need to make a directory. It will be a string object which includes the path and the directory name which needs to be created.

mode (optional): The permission that needs to be set to the newly-created directory. If you do not specify this parameter, then by default, it will set the permission as 0o777

exist_ok (optional): The default value is false, and if the directory exists, then os.makedir() will raise an FileExistsError.

Return Value – The os.mkdir() doesn’t return any value.

Example 1 – Create a directory in Python using os.makedirs()

The os.makedirs() will create the nested directory if the parent directory doesn’t exist in the specified path.

# Python program to create directory using os.makedirs() method import os # Directory path dir_path = "C:/Projects/Tryouts/test/sample/mydir" os.makedirs(dir_path) print("Directory '% s' created" % dir_path) # Directory path dir_path2 = "C:/Projects/Tryouts/test/sample/mydir2" # mode mode = 0o666 os.makedirs(dir_path2, mode) print("Directory '% s' created" % dir_path2) 
Directory 'C:/Projects/Tryouts/test/sample/mydir' created Directory 'C:/Projects/Tryouts/test/sample/mydir2' created

Example 2 – Exception if the directory already exists

The os.makedirs() method would raise a FileExistsError Exception if the directory in the location specified already exists.

# Python program to create directory using os.makedirs() method import os # Directory path dir_path = "C:/Projects/Tryouts/test/sample/mydir" os.makedirs(dir_path) print("Directory '% s' created" % dir_path)
Traceback (most recent call last): File "c:\Projects\Tryouts\main.py", line 7, in os.makedirs(dir_path) File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\os.py", line 225, in makedirs mkdir(name, mode) FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Projects/Tryouts/test/sample/mydir'

Источник

Create a directory in Python

The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. The os and os.path modules include many functions to interact with the file system. All functions in os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system.

There are different methods available in the OS module for creating a director. These are –

Using os.mkdir()

os.mkdir() method in Python is used to create a directory named path with the specified numeric mode. This method raise FileExistsError if the directory to be created already exists.

Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
mode (optional): A Integer value representing mode of the directory to be created. If this parameter is omitted then default value Oo777 is used.
dir_fd (optional): A file descriptor referring to a directory. The default value of this parameter is None.
If the specified path is absolute then dir_fd is ignored. Note: The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘dir_fd’) are keyword-only parameters and they can be provided using their name, not as positional parameter. Return Type: This method does not return any value.

Directory 'GeeksforGeeks' created Directory 'Geeks' created
Traceback (most recent call last): File "gfg.py", line 18, in os.mkdir(path) FileExistsError: [WinError 183] Cannot create a file when that file / /already exists: 'D:/Pycharm projects/GeeksForGeeks'
[WinError 183] Cannot create a file when that file/ /already exists: 'D:/Pycharm projects/GeeksForGeeks'

Using os.makedirs()

os.makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs() method will create them all.
For example, consider the following path:

D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil

Suppose we want to create directory ‘Nikhil’ but Directory ‘GeeksForGeeks’ and ‘Authors’ are unavailable in the path. Then os.makedirs() method will create all unavailable/missing directories in the specified path. ‘GeeksForGeeks’ and ‘Authors’ will be created first then ‘Nikhil’ directory will be created.

Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
mode (optional): A Integer value representing mode of the newly created directory. If this parameter is omitted then the default value Oo777 is used.
exist_ok (optional): A default value False is used for this parameter. If the target directory already exists an OSError is raised if its value is False otherwise not. Return Type: This method does not return any value.

Источник

Python | os.makedirs() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
All functions in os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system.
os.makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs() method will create them all.
For example consider the following path:

/home/User/Documents/GeeksForGeeks/Authors/ihritik

Suppose we want to create directory ‘ihritik’ but Directory ‘GeeksForGeeks’ and ‘Authors’ are unavailable in the path. Then os.makedirs() method will create all unavailable/missing directory in the specified path. ‘GeeksForGeeks’ and ‘Authors’ will be created first then ‘ihritik’ directory will be created.

Syntax: os.makedirs(path, mode = 0o777, exist_ok = False)
Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
mode (optional) : A Integer value representing mode of the newly created directory..If this parameter is omitted then the default value Oo777 is used.
exist_ok (optional) : A default value False is used for this parameter. If the target directory already exists an OSError is raised if its value is False otherwise not. For value True leaves directory unaltered.
Return Type: This method does not return any value.

Code #1: Use of os.makedirs() method to create directory

Источник

Оцените статью