Friday, 24 September 2021

Python: Create a directory

‘os.mkdir’ method is used to create a directory.

 

Signature

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

 

‘mkdir’ method raise FileExistsError, if the file already exist with given name.

 

create_dir.py

import os

def create_dir(dir_path):
    try:
        os.mkdir(dir_path)
        print('Directory created successfully!!!')
    except FileExistsError:
        print('File with given name ', dir_path, ' is already exists')
 

create_dir('/Users/Shared/poi/xls/test1')
create_dir('/Users/Shared/poi/xls/test1')

 

Output

Directory created successfully!!!
File with given name  /Users/Shared/poi/xls/test1  is already exists

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment