删除文件夹(目录)

def del_file(filepath):
    """
    删除某一目录下的所有文件或文件夹
    :param filepath: 路径
    :return:
    """
    del_list = os.listdir(filepath)
    for f in del_list:
        file_path = os.path.join(filepath, f)
        if os.path.isfile(file_path):
            os.remove(file_path)
        elif os.path.isdir(file_path):
            shutil.rmtree(file_path)

删除文件

if os.path.exists(index_html_path):  # 如果文件存在
    # 删除文件,可使用以下两种方法。
    os.remove(index_html_path)  
    #os.unlink(path)
else:
    print('no such file')  # 则返回文件不存在