def info(line): if output: output.write(line) output.write('\n') try: os.makedirs(path) except (OSError, IOError) as exc: if not os.path.isdir(path): info('Path already exists: %s' % path) else: raise else: info('Created directory %s' % path) segments = path.split(os.path.sep) for i in xrange(len(segments)): init_filename = os.path.sep.join(segments[:i+1] + ['__init__.py']) if not os.path.isfile(init_filename): try: open(init_filename, 'w').close() except (OSError, IOError) as exc: raise else: info('Created file %s' % (init_filename,)) else: info('File already exists: %s' % (init_filename,))