- Create a directory in Python
- Using os.mkdir()
- Using os.makedirs()
- Os mkdir mode python
- Process Management
- File Descriptor Operations
- Process Parameters
- Inheritance of File Descriptors
- Interface to the scheduler
- System Information
- Process Management
- File Descriptor Operations
- Python os.mkdir() Method
- Syntax
- Parameter Values
- Technical Details
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
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.
Os mkdir mode python
- Python | os.link() method
- Python | os.listdir() method
- Python | os.mkdir() method
- Python | os.makedirs() method
- Python | os.mkfifo() method
- Python | os.major() method
- Python | os.minor() method
- Python | os.makedev() method
- Python | os.readlink() method
- Python | os.remove() method
- Python | os.removedirs() method
- Python | os.rename() method
- Python | os.renames() method
- Python – os.replace() method
- Python | os.rmdir() method
- Python | os.scandir() method
- Python | os.stat() method
- Python | os.statvfs() method
- Python | os.sync() method
- Python | os.truncate() method
- Python | os.unlink() method
- os.walk() in Python
- Python | os.mkdir() method
- Python | os.abort() method
- Python | os._exit() method
- Python | os.fork() method
- Python | os.kill() method
- Python | os.nice() method
- Python | os.system() method
- Python | os.times() method
- Python | os.wait() method
- Python | os.mkdir() method
- Python | os.open() method
Process Management
File Descriptor Operations
- Python | os.get_blocking() method
- Python | os.isatty() method
- Python | os.openpty() method
- Python | os.pipe() method
- Python | os.pipe2() method
- Python | os.pread() method
- Python | os.write() method
- Python | os.pwrite() method
- Python | os.read() method
- Python | os.sendfile() method
- Python | os.set_blocking() method
Process Parameters
- Python | os.ctermid() method
- Python | os.environ object
- Python os.chdir() method
- Python | os.fchdir() method
- Python | os.getcwd() method
- Python | os.getenv() method
- Python | os.get_exec_path() method
- Python | os.geteuid() and seteuid() method
- Python | os.getgrouplist() method
- Python | os.getgroups() method
- Python | os.getlogin() method
- Python | os.getpgid() method
- Python | os.getpgrp() method
- Python | os.getpid() method
- Python | os.getppid() method
- Python | os.getresuid() and os.setresuid() method
- Python | os.getuid() and os.setuid() method
- Python | os.setregid() method
- Python | os.setreuid() method
- Python | os.setgroups() method
- Python | os.getsid() method
- Python | os.strerror() method
- Python | os.supports_bytes_environ object
- Python | os.umask() method
Inheritance of File Descriptors
Interface to the scheduler
System Information
- Python | os.link() method
- Python | os.listdir() method
- Python | os.mkdir() method
- Python | os.makedirs() method
- Python | os.mkfifo() method
- Python | os.major() method
- Python | os.minor() method
- Python | os.makedev() method
- Python | os.readlink() method
- Python | os.remove() method
- Python | os.removedirs() method
- Python | os.rename() method
- Python | os.renames() method
- Python – os.replace() method
- Python | os.rmdir() method
- Python | os.scandir() method
- Python | os.stat() method
- Python | os.statvfs() method
- Python | os.sync() method
- Python | os.truncate() method
- Python | os.unlink() method
- os.walk() in Python
- Python | os.mkdir() method
- Python | os.abort() method
- Python | os._exit() method
- Python | os.fork() method
- Python | os.kill() method
- Python | os.nice() method
- Python | os.system() method
- Python | os.times() method
- Python | os.wait() method
- Python | os.mkdir() method
- Python | os.open() method
Process Management
File Descriptor Operations
- Python | os.get_blocking() method
- Python | os.isatty() method
- Python | os.openpty() method
- Python | os.pipe() method
- Python | os.pipe2() method
- Python | os.pread() method
- Python | os.write() method
- Python | os.pwrite() method
- Python | os.read() method
- Python | os.sendfile() method
- Python | os.set_blocking() method
Python os.mkdir() Method
Note: Available on WINDOWS and UNIX platforms.
Syntax
Parameter Values
Parameter | Description |
---|---|
path | Required. A path-like object representing a file system path. The path-like object is either a string or bytes object representing a path |
mode | Optional. An integer value representing permission of the newly-created directory. The default value is Oo777 . |
dir_fd | Optional. A file descriptor referring to a directory. The default value is None |
Technical Details
Return Value: | None |
---|---|
Python Version: | 1.5.2 |
Change Log: | 3.3 — Added the dir_fd parameter 3.6 — Accepts a path-like object |
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.