How to create new folder?

This question already has an answer here:

  • How can I create a directory if it does not exist? 26 answers

  • You can create a folder with os.makedirs()
    and use os.path.exists() to see if it already exists:

    newpath = r'C:Program Filesarbitrary' 
    if not os.path.exists(newpath):
        os.makedirs(newpath)
    

    If you're trying to make an installer: Windows Installer does a lot of work for you.


    你可能想要os.makedirs,因为它也会创建中间目录,如果需要的话。

    import os
    
    #dir is not keyword
    def makemydir(whatever):
      try:
        os.makedirs(whatever)
      except OSError:
        pass
      # let exception propagate if we just can't
      # cd into the specified directory
      os.chdir(whatever)
    

    Have you tried os.mkdir?

    You might also try this little code snippet:

    mypath = ...
    if not os.path.isdir(mypath):
       os.makedirs(mypath)
    

    makedirs creates multiple levels of directories, if needed.

    链接地址: http://www.djcxy.com/p/9266.html

    上一篇: 使用try / except或if else创建和验证目录?

    下一篇: 如何创建新文件夹?