0

what's the shortest python oneliner for making sure a folder exists and if not creates it recursively? I can do it with two lines using the pathlib module

from pathlib import Path
Path('test/blah/foo/bar/').mkdir(parents=True, exist_ok=True)

can you do it in one line? having it in one line to use in various educational notebooks, where using two lines obscures the purpose of the code

Aviad Rozenhek
  • 2,259
  • 3
  • 21
  • 42
  • you can use `os.makedirs('test/blah/foo/bar/', exist_ok=True)` refer [here](https://docs.python.org/3/library/os.html#os.makedirs) – Shijith Dec 25 '19 at 13:41
  • 1
    @Shijith still requires import os – Aviad Rozenhek Dec 25 '19 at 13:49
  • 1
    @AviadRozenhek There is no way around it (unless you're doing it from an ipython console or a Jupyter notebook, which can natively supports shell commands) – Marat Dec 25 '19 at 14:13
  • There's no built-in that will do this—you're going to have to `import` something. Seems like a minor detail to me. – martineau Dec 25 '19 at 14:54

0 Answers0