mkdir_4.py 818 B

12345678910111213141516171819202122232425262728
  1. def info(line):
  2. if output:
  3. output.write(line)
  4. output.write('\n')
  5. try:
  6. os.makedirs(path)
  7. except (OSError, IOError) as exc:
  8. if not os.path.isdir(path):
  9. info('Path already exists: %s' % path)
  10. else:
  11. raise
  12. else:
  13. info('Created directory %s' % path)
  14. segments = path.split(os.path.sep)
  15. for i in xrange(len(segments)):
  16. init_filename = os.path.sep.join(segments[:i+1] + ['__init__.py'])
  17. if not os.path.isfile(init_filename):
  18. try:
  19. open(init_filename, 'w').close()
  20. except (OSError, IOError) as exc:
  21. raise
  22. else:
  23. info('Created file %s' % (init_filename,))
  24. else:
  25. info('File already exists: %s' % (init_filename,))