0

We can create a directory, only if it does not exist using

mkdir -p ./my_directory/

How can we achieve the same using os.mkdir() in python? os.mkdir("./my_directory/") fails if ./my_directory/ already exists and os.mkdir(" -p ./my_directory/") is not working.

brownser
  • 545
  • 7
  • 25
  • 1
    See: https://stackoverflow.com/questions/273192/how-can-i-safely-create-a-directory-possibly-including-intermediate-directories/14364249#14364249 – slothrop Mar 27 '23 at 16:47
  • 2
    Does this answer your question? [How can I safely create a directory (possibly including intermediate directories)?](https://stackoverflow.com/questions/273192/how-can-i-safely-create-a-directory-possibly-including-intermediate-directories) – JonSG Mar 27 '23 at 17:03
  • Just do like ```os.makedirs(..., exist_ok=True)```. It's the best solution currently. – relent95 Mar 28 '23 at 03:55

0 Answers0