Working With Directories in Python

Last modified: 
Monday, March 30th, 2015


This page outlines some techniques for working with directories in Python.

Creating Directories

How to create new directory in Python

import os, errno

path = '/path/to/new/directory'

How to create an entire directory structure

import os, errno

# Mimic behavior BASH mkdir -p in older versions of Python
def mkdir_p(path):
    except OSError, exc: # Python <= 2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
        else: raise

The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.