Search code examples
pythonfile-handling

Python Equivalent for Deno's ensureDir


What's the Python equivalent to Deno's ensureDir?

Usage example:

import { ensureDir, ensureDirSync } from "https://deno.land/std/fs/mod.ts";
ensureDir("./logs").then(
  () => console.log("Success Created"),
).catch((err) => console.log(err));

Solution

  • You can use pathlib.Path.mkdir. Example:

    import pathlib
    
    pathlib.Path.home().joinpath(".local", "extra", "path").mkdir(
        parents=True, exist_ok=True
    )