Monday, June 4, 2012

Creating a Single Directory or Multiple Directories

When automating processes, you should be very aware of system directory structures and the directory structure that your processes need to function properly.

The OS module has a collection of directory tools to make or remove directories as well as checking if they exist or not.

The standard make a directory function is the os.mkdir()

>>> import os
>>> directories = r"c:\temp3\level1"
>>> os.mkdir(directories)

The other folder creation tool I find very helpful is the os.makedirs(), which will generate a several folders at once.

>>> import os
>>> directories = r"c:\temp3\level1\level2\level3\level4"
>>> os.makedirs(directories)