24
loading...
This website collects cookies to deliver better user experience
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.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.os.mkdir()
doesn’t return any value.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
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 <module>
os.mkdir(dir_path)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Projects/Tryouts/sample'
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.os.makedir()
will raise an FileExistsError. os.mkdir()
doesn’t return any value.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
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 <module>
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'