What is different between makedirs and mkdir of os?

后端 未结 2 1120
挽巷
挽巷 2020-12-28 11:17

I am confused to use about these two osmethods to create the new directory.

Please give me some example in Python.

2条回答
  •  萌比男神i
    2020-12-28 11:56

    (Can not comment, just add to NPE's answer.)

    In Python3, os.makedirs has a default parameter exist_ok=False.
    If you set it to True, then os.makedirs will not throw any exception if the leaf exists.
    (While os.mkdir doesn't have this parameter.)

    Just like this:

    os.makedirs('dirA', exist_ok=True)

    P.S.
    You can type ? before the name of a method in IPython shell to take a quick look at the documentation.
    e.g.:

    >>> import os
    >>> ? os.makedirs
    

提交回复
热议问题