Always Get Better

Create Directory in Python

I needed to learn how to create directories using Python and found this great resource (digression: check out this author’s page headers – way cool!)

Although I didn’t really add anything new to the code, I ended up creating the following commented function which I share here in case it helps more users to understand.

[source:python]
# If directory (path) doesn’t exist, create it
def createPath(path):
if not os.path.isdir(path):
os.mkdir(path)

# Example of Windows file path
createPath( “C:\\Python\\myNewDirectory” )

# Example of Linux file path
createPath( “/home/username/myNewDirectory” )
[/source]

Tags: ,

One Response to “Create Directory in Python”

  1. […] Always Get Better Never stop looking for ways to improve « Create Directory in Python […]

Leave a Reply