How can I safely create a nested directory?

前端 未结 27 2678
旧时难觅i
旧时难觅i 2020-11-22 00:07

What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:

27条回答
  •  走了就别回头了
    2020-11-22 00:39

    If you consider the following:

    os.path.isdir('/tmp/dirname')
    

    means a directory (path) exists AND is a directory. So for me this way does what I need. So I can make sure it is folder (not a file) and exists.

提交回复
热议问题