How to overwrite a folder if it already exists when creating it with makedirs?

前端 未结 6 466
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 16:20

The following code allows me to create a directory if it does not already exist.

dir = \'path_to_my_folder\'
if not os.path.exists(dir):
    os.makedirs(dir)         


        
6条回答
  •  隐瞒了意图╮
    2020-12-23 16:56

    os.path.exists(dir) check is recommended but can be avoided by using ignore_errors

    dir = 'path_to_my_folder'
    shutil.rmtree(dir, ignore_errors=True)
    os.makedirs(dir)
    

提交回复
热议问题