python 实现:如果文件夹不存在就创建,如果文件存在就清空!
代码实现:
def setDir(filepath):
'''
如果文件夹不存在就创建,如果文件存在就清空!
:param filepath:需要创建的文件夹路径
:return:
'''
if not os.path.exists(filepath):
os.mkdir(filepath)
else:
shutil.rmtree(filepath,ignore_errors=True)
# os.mkdir(filepath)
函数解释
shutil.rmtree(filepath,ignore_errors=True)
删除整个filepath路径下的东西。如果ignore_errors = True
,则表示清楚失败导致的错误将会被忽略,如果ignore_errors = False
则表示清楚失败时将会调用由onerrors指定的程序来处理此类错误;如果忽略这一参数设定,清除失败时将会弹出一个exception。