-3

What is the best way to check if the directory a file is going to be written to exists, and if not, how to create the directory using Python?

Does a flag exists as "open", that makes this happen automatically?

Natsu
  • 1
  • 1
  • No, `open` didn't create a directory if that not exist. For creating a directory if not exist use @mx0 solution. – Ali Irani Jun 18 '21 at 08:25

1 Answers1

2

Use makedirs from os module:

os.makedirs(DIRECTORY, exist_ok=True)

This will create a directory if it doesn't exist.

mx0
  • 6,445
  • 12
  • 49
  • 54