Python, how to create a directory

To create a directory, useos.mkdir()Provided methodosStandard library modules:

import os

dirname = ‘/Users/flavio/test’

os.mkdir(dirname)

Creating a folder can triggerOSErrorException, for example, if the disk is full or the directory already exists, then we use a try block to catch it and handle the problem gracefully by printing an error message:

import os

dirname = ‘/Users/flavio/test’

try: os.mkdir(dirname) except OSError: print(‘Failed creating the directory’) else: print(‘Directory created’)


More python tutorials: