Skip to content

Instantly share code, notes, and snippets.

@pcarrier
Created June 27, 2011 16:05
Show Gist options
  • Save pcarrier/1049160 to your computer and use it in GitHub Desktop.
Save pcarrier/1049160 to your computer and use it in GitHub Desktop.
recursive_mkdir.rb
def self.mkdir_p(path)
path_acc = ''
path = File.join Dir.pwd, path if path[0] != ?/
path.split(File::SEPARATOR).grep(/./).collect do |name|
path_acc += File::SEPARATOR+ name
end.each do |dir|
begin Dir.mkdir(dir) rescue Errno::EEXIST end
end
end
@Daenyth
Copy link

Daenyth commented Jun 28, 2011

Python version:

def makedirs(path):
    """
    Make directories recursively.

    A wrapper for os.makedirs that won't raise OSError for existing directories
    Basically is like `mkdir -p'
    """
    try:
        os.makedirs(path)
    except OSError as e:
        if e.errno == errno.EEXIST:
            # Directory already exists
            pass
        else:
            raise

@pcarrier
Copy link
Author

OK, so in Python it's embedded in the standard library, almost as is :)

@pcarrier
Copy link
Author

Ah ah in Ruby too.
Well that was fun to write anyway.

require 'ftools'
File.makedirs ("/path/that/exists/or/not")

@Daenyth
Copy link

Daenyth commented Jun 28, 2011

Indeed. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment