import os
for files in os.listdir('output'):
        if files.endswith(".py"):
            os.remove(os.path.join('output',files))

下面是删除所有以及子目录

import os,shutil

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)
del_file('test_fold')

 

相关文章:

  • 2021-09-14
  • 2021-10-21
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
  • 2022-12-23
  • 2022-01-28
猜你喜欢
  • 2021-11-17
  • 2021-11-23
  • 2021-06-05
  • 2021-07-09
  • 2021-10-16
相关资源
相似解决方案