关键语句:

shutil.rmtree(dirpath) # 能删除该文件夹和文件夹下所有文件

其它:

如果文件夹不存在就创建,如果文件存在就清空

import os
import shutil
def setDir(filepath):
    '''
    如果文件夹不存在就创建,如果文件存在就清空!
    :param filepath:需要创建的文件夹路径
    :return:
    '''
    if not os.path.exists(filepath):
        os.mkdir(filepath)
    else:
        shutil.rmtree(filepath)
        os.mkdir(filepath)

 

相关文章:

  • 2022-02-06
  • 2021-05-31
  • 2021-12-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-28
  • 2022-01-03
  • 2021-11-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2022-02-06
  • 2021-07-01
  • 2022-12-23
  • 2021-12-20
相关资源
相似解决方案