Delete files with python through OS shell

前端 未结 4 422
时光取名叫无心
时光取名叫无心 2020-12-30 02:19

Im Tyring to Delete all Files in E:. with wildcard.

E:\\test\\*.txt

I would ask rather than test the os.walk. In windows.

4条回答
  •  误落风尘
    2020-12-30 02:46

    If you want to delete file with more than one extension then define those extensions in tuple like below

    import os
    
    def purge(dir):
        files = os.listdir(dir)
        ext = ('.txt', '.xml', '.json')
        for file in files:
            if file.endswith(ext):
                print("File -> " + os.path.join(dir,file))
                os.remove(os.path.join(dir,file))
    

提交回复
热议问题