apenwarr-redo/helpers.py

34 lines
754 B
Python
Raw Normal View History

import sys, os, errno
def unlink(f):
"""Delete a file at path 'f' if it currently exists.
Unlike os.unlink(), does not throw an exception if the file didn't already
exist.
"""
try:
os.unlink(f)
except OSError, e:
if e.errno == errno.ENOENT:
pass # it doesn't exist, that's what you asked for
2010-11-12 07:03:06 -08:00
def mkdirp(d, mode=None):
"""Recursively create directories on path 'd'.
Unlike os.makedirs(), it doesn't raise an exception if the last element of
the path already exists.
"""
try:
if mode:
os.makedirs(d, mode)
else:
os.makedirs(d)
except OSError, e:
if e.errno == errno.EEXIST:
pass
else:
raise