How can I safely create a nested directory?

前端 未结 27 3115
旧时难觅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:40

    import os
    if os.path.isfile(filename):
        print "file exists"
    else:
        "Your code here"
    

    Where your code here is use the (touch) command

    This will check if the file is there if it is not then it will create it.

提交回复
热议问题